Nothing appears when calling data from the database when uploading query - php

am using localhost and database from phpmyadmin
in php
<?php
$pdo = new PDO("mysql:host=localhost;port=3306;dbname=site_1", "root", "");
$query = "SELECT * FROM comment_v1";
$stmt = $pdo->prepare($query);
$avatars = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($avatars);
?>
in output
Array ( )
am working on games download site and i created 4 tables for now and all scripts working 100% without pb and the query the same but when i try to SELECT anything from table comment_v1 his apears nothing and idk the reason so i try to disable all the old query in scripts but the same result i got , but when i try code to SELECT the old query again his shows nothing with knowing that old query still working for now and idk why when i try to SELECT them again his show me nothing

Have you tried adding an execute() function before the fetchAll()?
something like:
<?php
$pdo = new PDO("mysql:host=localhost;port=3306;dbname=site_1", "root", "");
$query = "SELECT * FROM comment_v1";
$stmt = $pdo->prepare($query);
$stmt = $stmt->execute();
$avatars = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($avatars);
?>

Related

Problems with search query using php and mysql

i just want a direct answer and explanation why my prior query works but the latter does not..
here is the query that works just fine:
$sql = "SELECT * FROM productslist WHERE brand LIKE ?";
and this doesn't work at all and just returns an error:
$sql = 'SELECT * FROM productslist WHERE brand LIKE "%'.$search_string.'%"';
can someone please explain me why the latter query doesn;t work at all?
thanks in advance..
I tested the query with a table on my own DB. worked fine... I used a constant
Try mysqli_real_escape_string:
$search_string = mysqli_real_escape_string($conn, $search_string);
$sql = 'SELECT * FROM productslist WHERE brand LIKE "%'. $search_string .'%"';
Try this with PDO (Edit: Including PDO connection string since he didn't specify if he was using PDO) -
$dbh = new PDO("mysql:hostname=$your_server;dbname=$database_name, $username, $password);
$sql = "SELECT * FROM productslist WHERE brand LIKE :search";
$query = $dbh->prepare($sql);
$query->bindValue(":search", "%$search_string%");
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);

PDO Prepared Statements and MSSQL databases not functioning correctly

I'm having a problem running prepared queries on a MSSQL database using PDO. I can connect to the database and run SELECT queries with no parameters, but now I'm trying to run a simple SELECT query with one parameter, :user. However, the code does not return any values, despite the fact that there definitely is a database row with that value in. Here's the code I'm using:
$db = new PDO('dblib:host='.$dbHost.';dbname='.$dbName.';charset=utf8mb4',$dbUser, $dbPass);
$stmt = $db->prepare('SELECT * FROM customer WHERE email_address = :user ');
$stmt->bindValue(":user", $_SESSION["username"], PDO::PARAM_STR);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($result);
I receive no output from the var_dump. I know that in the database there is a correct row, so I tried:
$stmt = $db->prepare("SELECT * FROM customer WHERE email_address = 'the#email.com'");
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($result);
And yet still no value was returned. Am I doing something wrong with PDO? If I type this exact query into the query bar it runs.
you forgot to execute your query.
right after the paramter binding, put this code:
$stmt->execute();
Ok, I'm an idiot. Forgot to execute the query. Amended code for people in the same predicament:
$db = new PDO('dblib:host='.$dbHost.';dbname='.$dbName.';charset=utf8mb4',$dbUser, $dbPass);
$stmt = $db->prepare('SELECT * FROM customer WHERE email_address = :user ');
$stmt->bindValue(":user", $_SESSION["username"], PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($result);

How to exequte mssql query with function?

I want to run a mssql query that has a part of the user defined function.
This is my code:
$connection = $this->em->getConnection();
$sql = 'select * from ssm.table where ip = dbo.fn_ConvertIpAddressToBinary(\'192.168.0.1\')';
$result = $connection->executeQuery($sql);
$results = $result->fetchAll();
var_dump($results);
However, I get a blank response. If you set the request without function, everything works fine. This query works fine in sql query analyzer.
What can I do wrong?
try rewrite as:
$sql = "select * from ssm.table where ip = dbo.fn_ConvertIpAddressToBinary('192.168.0.1')";

How can I write a php code for data can not find in table of MySQL database?

I am so sorry mybe it is a silly question but as I am new in web language and php I dont know how to solve this problem.
I have a code which is getting ID from user and then connecting to MySQL and get data of that ID number from database table and then show on webpage.
But I would like to what should I add to this code if user enter an ID which is not in table of database shows a message that no data found.
Here is my code:
<?php
//connect to the server
$connect = mysql_connect ("localhost","Test","Test") ;
//connection to the database
mysql_select_db ("Test") ;
//query the database
$ID = $_GET['Textbox'];
$query = mysql_query (" SELECT * FROM track WHERE Code = ('$ID') ");
//fetch the results / convert results into an array
$ID = $_GET['Textbox'];
WHILE($rows = mysql_fetch_array($query)) :
$ID = 'ID';
echo "<p style=\"font-color: #ff0000;\"> $ID </p>";
endwhile;
?>
Thank You.
Sorry if it is so silly question.
You should use PDO (great tutorial here: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers ). This way, you can develop safer applications easier. You need to prepare the ID before inserting it to the query string, to avoid any user manipulation of the mysql query (it is called sql injection, guide: http://www.w3schools.com/sql/sql_injection.asp ).
The main answer to your question, after getting the results, you check if there is any row in the result, if you got no result, then there is no such an ID in the database. If you use PDO statements $stmt->rowCount();.
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
$stmt = $db->prepare("SELECT * FROM table WHERE Code=?");
$stmt->bindValue(1, $id, PDO::PARAM_INT); // or PDO::PARAM_STR
$stmt->execute();
$row_count = $stmt->rowCount();
if ($row_count > 0) {
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
//results are in $results
} else {
// no result, no such an ID, return the error to the user here.
}
Another reason to not use mysql_* functions: http://php.net/manual/en/migration55.deprecated.php

loop mysql results in php outside of mysql query

i am having a bit of issue with a mysql query. for some reason i can echo all all associated rows inside of the mysql query but outside of the query it only return the last row. here is my code. any suggestions?
//Get all associated
$q=mysql_query("SELECT * FROM `ACCOUNT` WHERE ACCOUNT_ID='$act_id'");
while ($row = mysql_fetch_assoc($q)){
$act_name=$row['ACT_NAME'];
echo "$act_name<br>"; // This returns all rows fine
}
echo "$act_name<br>"; // This only return the last row. i would like to get all rows.
The only way that you can fetch all of the records is by using PDO or MySQLi. Here is an example:
$conn = new mysqli($hostname, $username, $password, $database);
$query = "SELECT * FROM `ACCOUNT` WHERE ACCOUNT_ID='$act_id'";
$results = $conn->query($query);
$resultArray = $results->fetch_all(MYSQLI_ASSOC);
As #esqew said, you need to stop using the mysql_* functions.

Categories