473,609 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's wrong with my php query?

97 New Member
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
9 1503
computerfox
276 Contributor
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
tdrsam
97 New Member
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
276 Contributor
: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
tdrsam
97 New Member
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
276 Contributor
Would it be possible to post the full code?
Jun 5 '15 #6
tdrsam
97 New Member
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
276 Contributor
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
tdrsam
97 New Member
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
276 Contributor
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

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

Similar topics

3
2387
by: Chris Geerdink | last post by:
combo with PHP. what is wrong with the Javascript? else { include("mysql.php"); $query1 = mysql_query("INSERT INTO gbook (naam, email, text) VALUES ('".$_POST."', '".$_POST."', '".$_POST."')"); ?> <script language="JavaScript"> <!--
4
4060
by: asdf | last post by:
Hello! Can someone tell me whats wrong with this piece of code: Option Compare Database Option Explicit Sub retrieve() Dim rst As ADODB.Recordset Dim i As Integer
5
1978
by: Alexandre Martins | last post by:
Provider=Microsoft.Jet.OLEDB.4.0;UserId=Admin;Password=teste;Data Source=C:\Inetpub\wwwroot\inktoner\dados\db_inktoner.mdb;Persist Security Info=True I can't connect in my database ! whats wrong ?? tks
1
2455
by: aa | last post by:
When I am reading from local disk (d:), everithing is OK, but then I am reading from map disk I am geting the this error. Whats wrong. Thanks Server Error in '/Extra' Application. ---------------------------------------------------------------------------- ---- The specified user does not exist. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more...
3
2037
by: mahsa | last post by:
Hi do you know whats wrong with this code? <asp:HyperLink id="HLink_Help" runat="server" NavigateUrl='<%# "javascript:window.open('comments.aspx?id=1,width=500,height=600, scrollBars=yes');" %>'>Need Help?</asp:HyperLink> -- mahsa
4
3532
by: blah | last post by:
Hello everyone, Ive been trying to get my application to "click" on a button in another application using SendMessage, Ive gotten this far but Im not sure whats wrong with this code, here is the whole application (its small for testing purposes) and it seems that window wraps the text, at least when I preview this post: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;
1
2488
by: '~=_Slawek_=~' | last post by:
$DOW = (jddayofweek(unixtojd(mktime(1, 1, 1, $month, $day, $year)))+6)%7; $DOW= (jddayofweek(juliantojd($month, $day, $year))+6)%7; The results are supposed to be the same, but they are not. Whats wrong? Any clues?
0
1031
by: Jim Andersen | last post by:
I am using Microsoft.ApplicationBlocks.Data (v 2.0.0.0). I have this parameter array I pass to a stored procedure. The last one is an output parameter. So I did this: Line 1: arParms(8) = New SqlClient.SqlParameter("@MyParam", SqlDbType.Int, 4, ParameterDirection.Output) Line 2: arParms(8).Direction = ParameterDirection.Output
7
2223
by: Mike Barnard | last post by:
It's a simple test... VERY SIMPLE. But... In an external stlyesheet some attributes don't show. With the same styles cut and pasted to the test internally it works as expected. Anyone tell me why? Its probably sooooooo obvious, but it is 1.19 am! Thanks. www.thunderin.co.uk/
5
2313
by: islayer | last post by:
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just the number. whats wrong with my code? #!/usr/bin/perl use Fcntl; @array = (a..z); srand; foreach (1..5) { $name = int(rand scalar(@array));
0
8109
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8509
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8188
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8374
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5502
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4059
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2502
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1630
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.