PHP Need Needed - php

I want the result of that above query but instead of the result of the query , the query is itself printed , not the result of it ? why?
<?php
$host = 'localhost';
$user = 'root';
$passwd = '';
$database = 'p_database';
$connect = mysql_connect($host,$user,$passwd) or die("could not connect to database");
$query = "SELECT DATE(order_time) AS date, SUM(Quantity) AS total_sales
FROM ss_orders,ss_ordered_carts
GROUP BY date";
mysql_select_db($database);
$result = mysql_query ($query,$connect);
print "$query";
?>

you had printed the $query , so please modify your code
mysql_select_db($database,$connect);
$result = mysql_fetch_assoc(mysql_query($query));
print_r($result);

Use Print_r or echo instead of Print
<?php
$host = 'localhost';
$user = 'root';
$passwd = '';
$database = 'p_database';
$connect = mysql_connect($host,$user,$passwd) or die("could not connect to database");
$query = "SELECT DATE(order_time) AS date, SUM(Quantity) AS total_sales
FROM ss_orders,ss_ordered_carts
GROUP BY date";
mysql_select_db($database);
$result = mysql_query ($query,$connect);
while(mysql_fetch_array($result)){
echo $result[1];
}
?>

Related

MySQL in PHP - WHERE clause - not working

I have this code:
<?php
$user = $_COOKIE["user"];
$password = $_COOKIE["password"];
$localhost = "localhost";
$userdb = "xxxxx";
$passworddb = "xxxxx";
$database = "xxxxx";
$conn = mysqli_connect($localhost, $userdb, $passworddb, $database);
$vyber = "SELECT PASSWORD FROM Login WHERE User=".$user;
$result = mysqli_query($conn, $vyber);
echo $result;
?>
Cookie are set and if I use $vyber in database so everything is good. But there PHP write nothing. Can anybody tell, what I doing wrong? (Without comand $vyber every thing running perfect)
instead of,
echo $result
try to do that :
while ($row = mysqli_fetch_row($result)){
echo $row[0];
}
It is query error change query to :
$vyber = "SELECT PASSWORD FROM Login WHERE User='$user'";
if did not work use die function to dispaly error message :
mysqli_query($conn, $vyber) or die(mysqli_error($conn));
To fetch records :
while ($row = mysqli_fetch_array($result)){
echo $row[0];
}

echo something out MySQL database

I am trying to get a question with answers out of my database. I just want to get one thing out of the database and not with a row. I thought this would work but it puts out this: Resource id #4 can someone explains what I am missing.
Thanks :)
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('lotto');
$sql = 'SELECT id, vraag, AntwA, AntwB, AntwC, AntwD FROM vraag1';
$test = mysql_query($sql);
echo $test;
?>
As said at least 10000 times everywhere in internet, never use MySQL_ ! (If your are trying to learn something new by using tutorials over internet, don't use old ones)
I recommend to use PDO which is modern API in PHP and a lot more secure when using it correctly with prepared statement ! But you can also use MYSQLI which is more similar to the MYSQL !
You have to export your data from return array :
Using PDO :
$db = new PDO ("mysql:host=".$hostname.";dbname=".$dbname, $username, $password);
$query = $db -> prepare ("SELECT * FROM vraag1");
$query -> execute (array ());
$rows = $query -> fetchAll (PDO::FETCH_ASSOC);
foreach ($rows as $row)
{
echo $id = $row["id"];
echo $vraag = $row["vraag "];
echo $AntwA = $row["AntwA "];
echo $AntwB = $row["AntwB "];
echo $AntwC = $row["AntwC "];
echo $AntwD = $row["AntwD "];
}
Using MYSQLI :
$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$query = "SELECT * FROM vraag1";
$rows = mysqli_query($db, $query);
while($row = mysqli_fetch_assoc($rows))
{
echo $row["id"];
echo $row["vraag"];
echo $row["AntwA"];
echo $row["AntwB"];
echo $row["AntwC"];
echo $row["AntwD"];
}
First of all the mysql function you are using is depreciated and no longer supported. you should use mysqli or pdo instead with prepared statements.
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "lotto";
// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, vraag, AntwA, AntwB, AntwC, AntwD FROM vraag1";
$test = mysqli_query($conn, $sql);
if (mysqli_num_rows($test) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($test)) {
echo "ID : ".$row['id']."<br>";
echo "vraag :".$row['vraag']."<br>";
echo "AntwA :".$row['AntwA']."<br>";
echo "AntwB :".$row['AntwB']."<br>";
echo "AntwC :".$row['AntwC']."<br>";
echo "AntwD :".$row['AntwD']."<br>";
}
} else {
echo "no results found";
}
mysqli_close($conn);
?>
For select function mysql_query() returns a resource on success, or FALSE on error.
so your assignment statement
$test = mysql_query($sql);
assign the resource to $test.
if you want the data inside the resource you can do
while($row= mysql_fetch_assoc($test)):
print_r($row);
endwhile;
also you can access the $row['column_name']
If you want to return only one row you can do this limit in query
$sql = 'SELECT id, vraag, AntwA, AntwB, AntwC, AntwD FROM vraag1 limit 1';
You need to add something like the following:
while($row = mysql_fetch_array($result)){
echo $row['id'];
echo $row['vraag'];
echo $row['AntwA'];
echo $row['AntwB'];
echo $row['AntwC'];
echo $row['AntwD'];
}
use mysqli instead of mysql
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
mysqli_select_db('lotto');
$sql = 'SELECT id, vraag, AntwA, AntwB, AntwC, AntwD FROM vraag1';
$test = mysqli_query($sql);
echo $test;
?>

how to convert my sql query into json using php?

I have created a text box in which a query is entered and I need to run the query to display the values stored in the mysql table using php. Kindly help me with the approach of the project or any links that can help me. I have tried doing it by taking single query in php:
<?php
$username = "username";
$password = "password";
$host = "localhost";
$database="test";
$server = mysql_connect($host, $username, $password);
$connection = mysql_select_db($database, $server);
$myquery = "SELECT `date_joined`, `city` FROM `users`";
$query = mysql_query($myquery);
if ( ! $query ) {
echo mysql_error();
die;
}
$data = array();
for ($x = 0; $x < mysql_num_rows($query); $x++) {
$data[] = mysql_fetch_assoc($query);
}
echo json_encode($data);
mysql_close($server);
?>
But I want to get the json for every query that is being entered in the textbox. How do I handle that ?
Try this:
<?php
$username = "username";
$password = "password";
$host = "localhost";
$database="test";
$server = mysql_connect($host, $username, $password);
$connection = mysql_select_db($database, $server);
$myquery = "";
$myquery = $_POST["id_of_the_text_box"];
$query = mysql_query($myquery);
if ( ! $query ) {
echo mysql_error();
die;
}
$data = array();
for ($x = 0; $x < mysql_num_rows($query); $x++) {
$data[] = mysql_fetch_assoc($query);
}
echo json_encode($data);
mysql_close($server);
?>
But try to validate first the SQL entered in the textfield.

mysql_query error even though syntax of query is correct

$host = "localhost";
$dbname = "my_ales****";
$dbpassword = "";
$dbuser = "ales***";
$conn = mysqli_connect($host, $dbuser, $dbpassword, $dbname) or die("sukotto");
if ($_POST["invia_ricerca"]){
$nome = $_POST["nome"];
$query = " SELECT gruppo1 FROM elenco WHERE nome = '$nome' ";
$risultato = mysql_query($query, $conn) or die("suka");
echo $risultato;
}
If I write the query into phpmyadmin page it works. If I try to launch the query using mysql_query it doesn't work and print "suka" (die()). And if I try mysql_error() into die(), Nothing prints.
you are using mysqli_* functions for connection but in query you are using mysql_query.
it should by mysqli_query
change this
$risultato = mysql_query($query, $conn) or die("suka");
to
$risultato = mysqli_query($query, $conn) or die("suka");
=========== Edit====
$host = "localhost";
$dbname = "my_ales****";
$dbpassword = "";
$dbuser = "ales***";
$conn = mysqli_connect($host, $dbuser, $dbpassword, $dbname) or die("sukotto");
if ($_POST["invia_ricerca"]){
$nome = $_POST["nome"];
$query = " SELECT gruppo1 FROM elenco WHERE nome = '$nome' ";
$risultato = mysqli_query($query, $conn) or die("suka");
$row = $risultato->fetch_array(MYSQLI_NUM);
printf ("%s\n", $row[0]); //use %i for integer
}
$host = "localhost";
$dbname = "my_ales****";
$dbpassword = "";
$dbuser = "ales***";
$conn = mysqli_connect($host, $dbuser, $dbpassword, $dbname) or die("sukotto");
// Determine if a variable is set and is not NULL
if ( isset($_POST["invia_ricerca"]) )
{
// Escapes special characters in a string for use in an SQL
$nome = mysqli_real_escape_string($conn, $_POST["nome"]);
// Prepare Query
$query = sprintf("SELECT gruppo1 FROM elenco WHERE nome = '%s'", $nome);
// Performs a query on the database
$risultato = mysqli_query($query, $conn) or die( mysqli_error($conn) );
// Fetch a result row
while( $row = mysqli_fetch_array($risultato) )
{
printf("<p>%s</p>", $row['gruppo1']);
}
// Free result
mysqli_free_result($risultato)
}
PS. die("suka") รจ fantastico ;)
You have missed up mysqli with mysql. Try this:
$host = "localhost";
$dbname = "my_ales****";
$dbpassword = "";
$dbuser = "ales***";
$conn = mysqli_connect($host, $dbuser, $dbpassword, $dbname) or die("sukotto");
if ($_POST["invia_ricerca"]){
$nome = $_POST["nome"];
$query = " SELECT gruppo1 FROM elenco WHERE nome = '$nome' ";
$risultato = mysqli_query($conn,$query) or die("suka");
if ($risultato) {
while ($row = mysqli_fetch_assoc($risultato)) {
print_r($row);
}
mysqli_free_result($risultato);
}
}

Multi URL paramaters with PHP and SQL

<?php
$username = "username";
$password = "password";
$hostname = "localhost";
$database = "database";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db($database,$dbhandle)
or die("Could not select database");
$id = 0;
if(isset($_GET['Day'])){ $id = (int)$_GET['Day']; }
if(!$id){
$query = "SELECT * FROM `TimeTable`";
} else {
$query = "SELECT * FROM `TimeTable` WHERE `Day`='".$id."'";
}
$result = mysql_query($query);
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
or die(mysql_error());
print json_encode($rows);
?>
This code worked previously, but has now stopped, and is producing Parse error: syntax error, unexpected T_LOGICAL_OR in /Directory/TimeTable.php on line 27
I am also looking to add more parameters, (eg: Where Day = $id and Year = $Year )
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
or die(mysql_error());
This is a syntax error, doesn't know what to do with that or die() statement. Change to this:
$result = mysql_query($query);
if (!$result) {
die('Error');
}
while(...) {
}
I have looked up the new mysql functions an have changed to mysqli.
<?php
$username = "username";
$password = "password";
$hostname = "localhost";
$database = "database";
$link = mysqli_connect($hostname, $username, $password, $database);
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
$id= 0;
if(isset($_GET['Day'])){ $id=(int)$_GET['Day']; }
$year = 0;
if(isset($_GET['Year'])){ $year=(int)$_GET['Year'];}
if(!$id){
$query = "SELECT * FROM TimeTable";
} else {
if (!year) {
$query = "Select * FROM TimeTable";
} else {
$query = "SELECT * FROM TimeTable WHERE Day=$id AND Year=$year";
}
}
$rows = array();
//Perform JSON encode
if($result = mysqli_query($link, $query)){
while($r = mysqli_fetch_assoc($result)){
$rows[] = $r;
}
}
print json_encode($rows);
?>

Categories