423,682 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,682 IT Pros & Developers. It's quick & easy.

What's wrong with my php query?

P: 97
I don't know why I keep getting error messages for this line of code:

Expand|Select|Wrap|Line Numbers
  1. $result = bool mysqli::real_query ( string $query )or die(mysqli::$error());
  2.  
Is there some thing wrong with it? I've tried the object orientated style as well as the procedural style, but I get error messages with both.
Jun 4 '15 #1

✓ answered by computerfox

Okay.....

So I got the code to work. Try the changes.
I also cleaned up your code to be more readable.
To be honest, I can already see TONS of future issues with this "new and improved" version of MySQL and I have a number of years coding for it. Messiest thing!

Also, please remember that if you have passwords set to no, you need to use null for that parameter. Is there anyway you can revert to the normal MySQL? You do know that just because it's installed, doesn't mean you have to use it... Have you tried the old code?

Anyway... Here's the code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  $mysqli = mysqli_init();
  3.  if(!$mysqli){
  4.   print "MYSQLI failed...";
  5.  }
  6.  if(!$mysqli->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')){
  7.  }
  8.  if(!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)){
  9.   print 'Setting MYSQLI_OPT_CONNECT_TIMEOUT failed';
  10.  }
  11.  $mysqli->real_connect('localhost', 'root',null,'pca');
  12.  if($mysqli->connect_error){
  13.   print 'Connect Error '.$mysqli->connect_error;
  14.  }
  15.  $query="select * from news";
  16.  $result=$mysqli->query($query);
  17.  print "<table class='displayReviews' border='1' style='width:100%;'>";
  18.  print "<tr stlye='display:block;margin:0em auto;'><th>date</th><th>Headline</th><th>Body</th><th>Image</th></tr>";
  19.  
  20.  while($row=$result->fetch_assoc()){
  21.   print "<tr><td>";
  22.   print $row['date'];
  23.   print "</td>";
  24.   print "</td><td>";
  25.   print $row['headline'];
  26.   print "</td><td>";
  27.   print $row['body'];
  28.   print "</td><td>";
  29.   print $row['image'];
  30.   print "</td><td>";
  31.   print '<a href="edit.php">Edit</a>';
  32.   print "</td><td>";
  33.   print '<a href="delete.php">Delete</a>';
  34.   print "</td></tr>";*/
  35.  }
  36.  print "</table>";
  37.  $mysqli->close();
  38. ?>
  39.  
http://cp.abelgancsos.com/project.php?id=405

If you must use the this version, this should be under your pillow:
http://php.net/manual/en/book.mysqli.php

Good luck!

To answer your other question, it's a web standard to use tables when showing critical data as crawlers can't access the tables. I have designed and implemented a few special GUI's that use divs instead of tables, but you need to know how to prevent robots from crawling the pages (or be willing to put the site on your intranet instead) and understand how the design should work. Stay with tables, it might not look nice, but it's a web standard and tons of organizations use them. What you could do is spend hours styling the table with CSS.

Also, I just noticed that the edit and delete pages won't do anything as it's just going to the page. You should be passing an identifier for the row. May I ask what you're writing this for?

Share this Question
Share on Google+
9 Replies


computerfox
100+
P: 278
What exactly are you trying to do?
Normally to run a MySQL query in PHP, you would so something like:
Expand|Select|Wrap|Line Numbers
  1. $getter=mysql_query($sql) or die(mysql_error)));
  2. while($d=mysql_fetch_assoc($getter)){
  3. }
  4.  
Jun 4 '15 #2

P: 97
I'm trying to bring all the records from a table in the database and display them on the web page. I used the method with mysql_query etc on my last website and had a lot of trouble when it came it deploying the site to a live server, so I'm trying to use the new and improved version with the mysqli extensions instead. I think it needs more detail as I keep getting error messages saying that the lines of code can't be used statically.
Jun 4 '15 #3

computerfox
100+
P: 278
:facepalm: Because it's standard database practice to loop through the results. No database, as far as I know, can grab all the records into an array the way you're trying, especially since you're trying to make it into a bool. That's just incorrect....

Try this:
Expand|Select|Wrap|Line Numbers
  1. $getter=mysql_query($sql);
  2. while($d=mysql_fetch_assoc($getter)){
  3.  print $d[column_name];
  4. }
  5.  
No matter if you use the old version or the "new and fancier" mysql, you still need to loop through to print the results.

Edit:
SQLite can, but you're not using SQLite.
Jun 5 '15 #4

P: 97
Thanks, but I am using a loop. A while loop to be exact. I just didn't add all the code to the question because I thought it was a simple question that someone would know the answer to right away. The problem is with the new MySQL extension. I'm fairly sure there must be something that I'm missing.
Jun 5 '15 #5

computerfox
100+
P: 278
Would it be possible to post the full code?
Jun 5 '15 #6

P: 97
Yes.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $mysqli = mysqli_init();
  3. if (!$mysqli) {die('mysqli_init failed');}
  4.  
  5. if (!$mysqli->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {die('Setting MYSQLI_INIT_COMMAND failed');}
  6.  
  7. if (!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');}
  8.  
  9. if (!$mysqli->real_connect('localhost', 'root', '', 'pca')) {die('Connect Error (' . mysqli_connect_errno() . ') '.mysqli_connect_error());}
  10.  
  11. $query = "select * from news";
  12.  
  13. $result = bool mysqli->real_query ( string $query )or die(mysqli->$error());
  14.  
  15. $row = mysqli_result::fetch_array($result);
  16.  
  17. echo "<table class='displayReviews' border='1' style='width:100%;'>";
  18.  
  19. echo "<tr stlye='display:block;margin:0em auto;'><th>date</th><th>Headline</th><th>Body</th><th>Image</th></tr>";
  20.  
  21. while ($row = mysqli_result::fetch_array($result))
  22.  
  23. {
  24. echo "<tr><td>"; 
  25. echo $row['date'];
  26. echo "</td><td>";
  27. echo $row['headline'];
  28. echo "</td><td>";
  29. echo $row['body'];
  30. echo "</td><td>";
  31. echo $row['image'];
  32. echo "</td><td>";
  33. echo '<a href="edit.php">Edit</a>';
  34. echo "</td><td>";
  35. echo '<a href="delete.php">Delete</a>';  
  36. echo "</td></tr>";
  37. }
  38. echo "</table>";
  39.  
  40. $mysqli->close();
  41.  
  42. ?> 
  43.  
Also, is there a better way than just sticking it in a table as well? I know tables are a bit old fashioned, but I suppose they still work ok.
Jun 5 '15 #7

computerfox
100+
P: 278
Okay.....

So I got the code to work. Try the changes.
I also cleaned up your code to be more readable.
To be honest, I can already see TONS of future issues with this "new and improved" version of MySQL and I have a number of years coding for it. Messiest thing!

Also, please remember that if you have passwords set to no, you need to use null for that parameter. Is there anyway you can revert to the normal MySQL? You do know that just because it's installed, doesn't mean you have to use it... Have you tried the old code?

Anyway... Here's the code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  $mysqli = mysqli_init();
  3.  if(!$mysqli){
  4.   print "MYSQLI failed...";
  5.  }
  6.  if(!$mysqli->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')){
  7.  }
  8.  if(!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)){
  9.   print 'Setting MYSQLI_OPT_CONNECT_TIMEOUT failed';
  10.  }
  11.  $mysqli->real_connect('localhost', 'root',null,'pca');
  12.  if($mysqli->connect_error){
  13.   print 'Connect Error '.$mysqli->connect_error;
  14.  }
  15.  $query="select * from news";
  16.  $result=$mysqli->query($query);
  17.  print "<table class='displayReviews' border='1' style='width:100%;'>";
  18.  print "<tr stlye='display:block;margin:0em auto;'><th>date</th><th>Headline</th><th>Body</th><th>Image</th></tr>";
  19.  
  20.  while($row=$result->fetch_assoc()){
  21.   print "<tr><td>";
  22.   print $row['date'];
  23.   print "</td>";
  24.   print "</td><td>";
  25.   print $row['headline'];
  26.   print "</td><td>";
  27.   print $row['body'];
  28.   print "</td><td>";
  29.   print $row['image'];
  30.   print "</td><td>";
  31.   print '<a href="edit.php">Edit</a>';
  32.   print "</td><td>";
  33.   print '<a href="delete.php">Delete</a>';
  34.   print "</td></tr>";*/
  35.  }
  36.  print "</table>";
  37.  $mysqli->close();
  38. ?>
  39.  
http://cp.abelgancsos.com/project.php?id=405

If you must use the this version, this should be under your pillow:
http://php.net/manual/en/book.mysqli.php

Good luck!

To answer your other question, it's a web standard to use tables when showing critical data as crawlers can't access the tables. I have designed and implemented a few special GUI's that use divs instead of tables, but you need to know how to prevent robots from crawling the pages (or be willing to put the site on your intranet instead) and understand how the design should work. Stay with tables, it might not look nice, but it's a web standard and tons of organizations use them. What you could do is spend hours styling the table with CSS.

Also, I just noticed that the edit and delete pages won't do anything as it's just going to the page. You should be passing an identifier for the row. May I ask what you're writing this for?
Jun 5 '15 #8

P: 97
Thanks for the help Computerfox, that seems to have gotten it going.

The password thing is only because the site is still in development, that password will have to change once it goes into production.

I'm not really sure about which version of MySQL to use. I've had people telling me off for using the old version, now I have you saying it's better, but I don't really have enough experience yet to know which is better.

Thanks for the link to that manual, I've been trying to use it, but it doesn't seem to be working for me.

The table is fine. I heard someone saying they're very old fashioned but I think that was for general building in html, which I wouldn't really do in tables. I'm fine with having my database retrieved data in tables.

I'll be getting to the edit and delete pages next. I'm writing this for a new responsive site for my company. This is part of the news page. News items will be stored in the database, then displayed on the news page in the site. And, there's an admin section where the news items are generated.

Thanks again.
Jun 5 '15 #9

computerfox
100+
P: 278
Anytime. Please mark the question as answered.
As an added note, I understand. Some developers like going with the newest and coolest stuff, but it's not always NEEDED to jump on the wagon. The "old" version of MySQL was actually stable and functional. I believe some of the reasons they changed it was to enable OOP style coding and of course prevent database injections. I've been using the same version since I built my server and even wrote API's for the database and all is fine.

A responsible developer tries not to fix what's not broken. I bet all your old code needed was some styling and it could have looked really good.

Anyway, I'm rambling. Please mark the question as answered and have a great night.
Jun 5 '15 #10

Post your reply

Sign in to post your reply or Sign up for a free account.