Solved

PHP MySQL query problem

Posted on 2014-03-10
6
598 Views
Last Modified: 2014-03-10
This works:
 $result = mysqli_query($con,"SELECT * FROM `Persons` WHERE PhoneNo='5555555555'");
 while ($row = mysqli_fetch_row($result)) 
   {
      echo $row[0] . " " . $row[1];
   }

Open in new window

(Phone number changed for privacy.)

This doesn't work:
$number = '5555555555';
$result = mysqli_query($MySQLcon,"SELECT * FROM 'Persons' WHERE PhoneNo='" . $number . "'");

Open in new window

What am I doing wrong? I also tried removing the single quotes around $number.

BTW: Is there a better way to retrieve the fields if I am selecting only one record than this?
while ($row = mysqli_fetch_row($result)) 
   {
	echo $row[0] . " " . $row[1];
   }

Open in new window

0
Comment
Question by:thenelson
6 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 350 total points
ID: 39918444
It's because your table in the second example is enclosed in single quotes, not backticks:

...FROM 'Persons'

Try changing it to:

FROM `Persons`
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39918446
You have straight quotes on the second example. That's all the difference I see.
Try
$number = '5555555555';
$result = mysqli_query($MySQLcon,"SELECT * FROM `Persons` WHERE `PhoneNo`='" . $number . "'");

Open in new window


HTH,
Dan
0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 150 total points
ID: 39918448
$row = $result->fetch_assoc()
Then just access each column as normal.
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39918551
The MySQLi functions return a value.  You can test this value to determine if the query worked or failed.  If it failed you can print out the MySQLi_error() data.

In the first case, it says this:
mysqli_query($con...

Open in new window

In the second case it says this:
mysqli_query($MySQLcon...

Open in new window

So maybe the connection name is incorrect?

Here is how I might write it so that I could get diagnostic information
error_reporting(E_ALL);
$number = '5555555555';
$query  = "SELECT * FROM Persons WHERE PhoneNo='$number' LIMIT 1";
$result = mysqli_query($con, $query);
if (!$result)
{
    echo "FAIL: $query <br>";
    trigger_error(mysqli_error(), E_USER_ERROR);
}

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39918560
a better way to retrieve the fields if I am selecting only one record
Yes, I would just do this, omitting the iterator:
if (!$result)
{
    echo "FAIL: $query <br>";
    trigger_error(mysqli_error(), E_USER_ERROR);
}
$row = mysqli_fetch_object($result);
print_r($row);

Open in new window

The advantage of the fetch_object method is simpler punctuation, fewer fiddly quotation marks, ease of integration into HTML templates that are created with HEREDOC, etc.
0
 
LVL 39

Author Closing Comment

by:thenelson
ID: 39918828
Yep, that was it. I guess being new to php and MySQL and my old eyes, I didn't see the difference between single quotes and back tics - thanks.

I guess I should have asked about $row = $result->fetch_assoc() as a separate question instead of a BTW but here is some points for that. Thanks
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question