?
Solved

PHP MySQL query problem

Posted on 2014-03-10
6
Medium Priority
?
620 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 1400 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 35

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 600 total points
ID: 39918448
$row = $result->fetch_assoc()
Then just access each column as normal.
0
Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

 
LVL 111

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 111

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 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