Nothing shows up with WHERE clause mySQL - php

I'm trying to put through a basic search through my Database based on a user entry, and I could not get it to work, so I'm doing a hard coded search just so I can get it to work... and I can't. My code is as follows:
<?php
$db_connection = mysqli_connect("HOST", "USERNAME", "PASSWORD", "DATABASE");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$data = mysql_query($db_connection, "SELECT * FROM card_collection WHERE ID='1207409700'");
while( $card = mysql_fetch_array( $data ) ) {
echo $card['Name'], '<br />';
}
mysql_close();
?>
I've double checked spelling for everything linked to the database. The ID I'm basing my search from is the first item in my database.

You are using the mysqli_connect to database and then you are using the mysql_query to execute the query it should be mysqli_query
You code should be:
$data = mysqli_query($db_connection, "SELECT * FROM card_collection WHERE ID='1207409700'");
while( $card = mysqli_fetch_array( $data ) ) {
echo $card['Name'], '<br />';
}
mysqli_close();
Replace the mysql with the mysqli. This may be your problem.

I personally use PDO and not mysqli, but is the problem perhaps that you use mysql_query and mysql_fetch_array, instead of mysqli_query and mysqli_fetch_array? Note the missing i
Edit: dangit, beat to the punch :)

<?php
$db_connect = mysqli_connect("host", "user", "pass", "db");
if($db_connect){
$data = mysqli_query($db_connection, "SELECT * FROM card_collection WHERE ID='1207409700'");}
else {"Not Connected to db";}
while( $card = mysqli_fetch_array( $data ) ) {
echo $card['Name'], '<br />';
}
mysqli_close();
?>

Related

This mysql_connect Attribute seems to old how can I replace them?

I cannot connect to my Data Base using this way:
mysql_connect("localhost","root","") or die("1");
mysql_select_db("database") or die("2");
After the connection to the data base I would like to create a while loop with the elements of the data base, this is my code:
$carlistreq = mysql_query("SELECT * FROM cars WHERE brand="'.$_GET['marq'].'"") or die("3");
$count = mysql_num_rows($carlistreq);
if($count==0)
{
echo "No elements";
}
else
{
while($row= mysql_fetch_array($query))
{
//my code
}
}
The issue comes from my SQL attributes like:
mysql_connect
mysql_fetch_array
mysql_select_db
For example.
Here is the error:
Deprecated: mysql_connect(): The mysql extension is deprecated and
will be removed in the future: use mysqli or PDO instead in C:[....]
on line 2
use the mysqli-extension like this:
$mysqli = new mysqli("localhost","user","password","database") or die("1");
$sql = "SELECT * FROM cars WHERE brand='".$_GET['marq']."'";
$result = $mysqli->query($sql);
if($result){
while($row = $result->fetch_assoc()){
//do something with your $row here
}
}
http://php.net/manual/en/book.mysqli.php
Use PDO
$db = new PDO('mysql:host=localhost;dbname=databaseName', 'User' , 'password');
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
Yes now servers are going to handel mysqli and PDO. Try implementing this code:
<?php
$con = mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
// Change database to "test"
mysqli_select_db($con,"test");
// ...some PHP code for database "test"...
mysqli_close($con);
?>
You can user http://php.net/manual/en/book.mysqli.php as reference to understand better how mysqli works.

Connect to my database with PHP

I want to connect my HTML page to my database but I”m not getting it not sure why. What’s wrong with the code?
<?php
$con = mysqli_connect("localhost","root","","sibd02");
$query = sprintf("SELECT*FROM fornecedor ",
$result = mysql_query($query);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_close($con);
?>
There are lots of issues with your code. Here is my cleaned up version of your code which should work:
// Set the connection or die returning an error.
$con = mysqli_connect("localhost", "root", "sibd02") or die(mysqli_connect_errno());
// Set the query.
$query = "SELECT * FROM fornecedor";
// Run the query.
$result = mysqli_query($con, $query) or die(mysqli_connect_errno());
// Print the result for initial testing.
echo '<pre>';
print_r($result);
echo '</pre>';
// Free the result set.
mysqli_free_result($result);
// Close the connection.
mysqli_close($con);
So let’s look at your original code with notes below on each issue:
$con = mysqli_connect("localhost","root","","sibd02");
$query = sprintf("SELECT*FROM fornecedor ",
$result = mysql_query($query);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_close($con);
First this line $query = sprintf("SELECT*FROM fornecedor ", is incorrect syntax in PHP. And the sprintf is not needed.
Then your MySQL query would fail because it has it’s own syntax error: SELECT*FROM fornecedor That SELECT*FROM should have spaces like this SELECT * FROM.
Then you are using mysqli_connect for the connection but then using mysql_query for the query. Those are two 100% different functions in different classes. Mixing them like this will never work.
Mixing MySQL and MySQLi won't work. You can simply do this:
<?php
/* ESTABLISH CONNECTION */
$con=mysqli_connect("localhost","root","","sibd02");
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
$query = "SELECT * FROM fornecedor";
$result = mysqli_query($con,$query); /* EXECUTE QUERY */
/* IF YOU WANT TO PRINT THE RESULTS, HERE IS AN EXAMPLE: */
while($row=mysqli_fetch_array($result)){
echo $row['column']." ".$row['column2']." ".$row['column3']; /* JUST REPLACE THE NECESSARY COLUMN NAME */
} /* END OF WHILE LOOP */
mysqli_close($con);
?>

PHP: connected to database, but mysql_query fails

I have very strange problem with PHP which I am starting to learn .. I have created tables in MySQL database with some data, and now I want to show them in webpage.
This is my source where I have this problem:
<?php
// Here I open connection
$con = mysql_connect("localhost","root","aaaaaa");
// set the mysql database
$db = mysql_select_db("infs", $con);
// I check the connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
// It always goes here
echo "Connected to database!";
}
// I am testing very simple SQL query.. there should be no problem
$result = mysql_query("SELECT * FROM cathegories", $con, $db);
if (!$result) {
// but it always dies
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
mysql_close($con);
?>
What is wrong?
Thanks in advance!
You are mixing mysql and mysqli.
Try something like:
<?php
$con= new mysqli("localhost","user","passwd","database");
if ($con->connect_errno){
echo "could not connect";
}
$select = "SELECT * FROM tablename";
if($result = $con->query($select)){
while($row = $result->fetch_object()){
echo $row->rowname."<br>";
}
}
else { echo 'no result'; }
$con->close();
?>
// Here I open connection
$con = mysql_connect("localhost","root","aaaaaa");
// set the mysql database
$db = mysql_select_db("infs", $connection);
change to
// Here I open connection
$con = mysql_connect("localhost","root","aaaaaa");
// set the mysql database
$db = mysql_select_db("infs", $con);
mysql_query only takes two parameters - the actual SQL and then the link identifier (I assume in your case that's stored in $con; therefore remove $db from the third parameter).
You don't even need the second $con parameter really.
Where's the actual logic to connect to the database initially? Just because mysqli_connect_errno() doesn't return an error it doesn't mean the connection actually exists and that $con is available in the current scope.
I'd var_dump($con) before the mysql query to make sure it's a valid connection.

php- cant get result from mysql (mysqli)

I'm attempting to set up an API for an app project.
I've got a mysql table called 'users', which I've added a row to.
using the code:
// Create connection
$mysqli = new mysqli("localhost","user", "pass", "db");
// Check connection
if($mysqli->connect_errno){
$result = "Failed to connect to MySQL: " . mysqli_connect_error();
print_r( json_encode($result) );
return false;
}
$row = $mysqli->query("SELECT * FROM users");
print_r( json_encode($row) );
I get an empty result, how come? (connection doesn't throw an error)
to be exact i get:
{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}
EDIT:
got the answer to ym original question, thanks!
so now using the code:
$row = $mysqli->query("SELECT * FROM users WHERE email = '".$email."'");
$result = $row->fetch_array();
print_r( json_encode($result) );
I get the result:
{"0":"test","username":"test","1":"test#test.com","email":"test#test.com","2":"test","password":"test","3":"2013-10-18 22:22:53","date_registered":"2013-10-18 22:22:53","4":"1","id":"1"}
where what i want is something like:
{"username":"test","password":"test","email":"test#test.com", ...etc }
how do i get that?
Try this:
$result = $mysqli->query("SELECT * FROM users");
$row = $result->fetch_array(MYSQLI_ASSOC);
print json_encode($row); // json_encode returns a string...
Try this for your associative array:
while($row = $result->fetch_array(MYSQLI_ASSOC))
{
$rows[] = $row;
}
print json_encode($rows);
or you can try... $rows = $result->fetch_all(MYSQLI_ASSOC);
mysqli_query() will return a mysqli_result, you need to fetch (as an array in this case) your rows before doing anything with them. Added a line:
// Create connection
$mysqli = new mysqli("localhost","user", "pass", "db");
// Check connection
if($mysqli->connect_errno){
$result = "Failed to connect to MySQL: " . mysqli_connect_error();
print_r( json_encode($result) );
return false;
}
// Get a mysql_result
$row = $mysqli->query("SELECT * FROM users");
// Get it into an array without numeric indexes
$result = $row->fetch_assoc();
// Display the row
print_r( json_encode($result) );

PHP variable not displaying value

I am storing MySQL Query value in PHP variable, but its not displaying data. P.S: Data is available in MySQL table column.
<?php
$cmsca= mysql_query("SELECT SUM(qa_effort) FROM tbl_uat WHERE product='CAP'");
while ($cresulta = mysql_fetch_array ($cmsca))
$arra[0] = $cresulta[0];
echo $arra[0];
?>
I am out of clues, what is wrong in above code? Need help!
Regards
try this
<?php
$cmsca= mysql_query("SELECT SUM(qa_effort) as sums FROM tbl_uat WHERE product='CAP'");
while ($cresulta = mysql_fetch_array($cmsca))
{
echo $cresulta['sums'];
}
?>
first of all, do not use mysql_query, - it's deprecated, use http://www.php.net/manual/en/mysqli.query.php instead.
Next, you need to connect to the db, before running query;
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$result = mysqli->query("SELECT SUM(qa_effort) as sums FROM tbl_uat WHERE product='CAP'");
while ($row = $result->fetch_array()) {
var_dump($row);
}
$mysqli->close();
?>
How about trying this:
<?php
$arra = array();
$cmsca= mysql_query("SELECT SUM(qa_effort) FROM tbl_uat WHERE product='CAP'");
while ($row = mysql_fetch_array ($cmsca))
$arra = $row;
print_r($arra);
?>

Categories