mysql get data from outside of while - php

I want to get some data from database with this code
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "test";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password);
mysql_select_db($mysql_database, $bd);
$result = mysql_query("SELECT author_id FROM user");
while($data = mysql_fetch_array($result)) {
$sn=$data['author_id'];
}
$lastresult = mysql_query("SELECT * FROM post WHERE id='".$sn."'");
This code only work fetches one author_id
$sn=$data['author_id'] and $lastresult works only with one author_id and doesn't fetches all the author_id.
How can I get data for all author_id ?

You are overwritting $sn each loop.
$result = mysql_query("SELECT author_id FROM user");
$sn = [];
while($data = mysql_fetch_array($result)) {
$sn[] = $data['author_id'];
}
$sn = implode(',',$sn);
$lastresult = mysql_query("SELECT * FROM post WHERE id IN (".$sn.")");
PS: mysql_* functions are deprecated and has security issues. Consider replace with mysqli_* functions or PDO.

Related

PHP mysqli->prepare returning nothing

I'm trying to use a mysqli connection to retrieve rows from my database however I continue to receive a 500 internal server error no matter what I try.
$getUserQuery = "SELECT * FROM members WHERE name = ? AND member_id > 0";
$getUserStatement = $mysqli_conn->prepare($getUserQuery);
$getUserStatement->bind_param("s", $name);
$mysqli_conn->ping() results in a value of true so I know there's no issue with the database connection. var_dump($getUserStatement) results in bool(false) so there's some issue with the prepare. Whole code:
$user_dirty = $_GET['u'];
$pass_dirty = $_GET['p'];
$getUserQuery = "SELECT * FROM members WHERE name = ? AND member_id > 0";
$getUserStatement = $mysqli_conn->prepare($getUserQuery);
if ($getUserStatement) {
echo($getUserStatement);
} else {
echo("not good");
}
$getUserStatement->bind_param("s", $user_dirty);
$getUserStatement->execute();
$getUserResult = $getUserStatement->get_result();
And how I create my DB connection:
$mysql_host = "host";
$mysql_user = "user";
$mysql_password = "pass";
$mysql_database = "db";
$mysqli_conn = new mysqli($mysql_host, $mysql_user, $mysql_password, $mysql_database);
You gets 500 internal error because statement is false probably(https://stackoverflow.com/a/845025/2962442 turn on display errors), not resource like it should be, it can be caused by lost connection, try ping or reconnect before prepare.
Example good code:
$getUserQuery = "SELECT * FROM members WHERE name = ? AND member_id > 0";
$getUserStatement = $mysqli_conn->prepare($getUserQuery);
if ($getUserStatement) {
$getUserStatement->bind_param("s", $name);
} else {
//statement is false, not good
}
and good example of connection part
$mysql_host = "host";
$mysql_user = "user";
$mysql_password = "pass";
$mysql_database = "db";
$mysqli_conn = new mysqli($mysql_host, $mysql_user, $mysql_password, $mysql_database);
if (!$mysqli_conn) {
//not connected...
}

How do I display the value of a row?

I have five rows setup in a MySQLi database:
$title, $subtitle, $owner, $contactemail, and $footer.
Instead of writing out my websites title on EVERY .php page, I would like to use a function that will display the value of $title, $subtitle, etc.
This is what I have done so far:
index.php
<?php include 'config.php'; ?>
<?echo $title;?>
config.php
<?php
$mysql_hostname = 'localhost';
$mysql_username = 'root';
$mysql_dbname = 'toplist';
$mysql_password = '';
$dbh= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$query = "SELECT * FROM toplist_settings";
$title=['title'];
$subtitle=['subtitle'];
$owner=['owner'];
$contactemail=['contactemail'];
$footer=['footer'];
?>
But this is not working. When I use , etc. nothing shows up.
try this
$query = "SELECT * FROM toplist_settings";
$stmt = $dbh->query($query);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$title=$row['title'];
$subtitle=$row['subtitle'];
$owner=$row['owner'];
$contactemail=$row['contactemail'];
$footer=$row['footer'];
execute your query to get records
$query = $dbh->prepare("SELECT * FROM toplist_settings");
print_r( $query->execute());

PHP Need Needed

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];
}
?>

grabbing variables from database in while loop

Im trying to add create a product page and I have all the relevant column names in the product table but the loop won't work for some reason :(
<?php
$dbhost = 'localhost';
$dbuser = 'CU4507408';
$dbpass = '*********';
$dbname = 'CU4507408';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to database");
mysql_select_db($dbname);
$query = "SELECT * FROM product WHERE id=".$_REQUEST['productID'].";";
$result = mysql_query($query) or die("failed!");
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$productID = $row['productID'];
$name = $row['name'];
$price = $row['price'];
$manufacturer = $row['manufacturer'];
$rating = $row['rating'];
$categoryID = $row['categoryID'];
$productinfo = $row['productinfo'];
$image = $row['image'];
$youtube = $row['youtube'];
}
?>
There error I get is
Undefined index: productID in /home/4507408/public_html/viewproduct.php on line 8 failed!
productID is the primary key of the product table
Thanks :)
$_REQUEST['productID'] is undefined.
Your URL (if you're using GET), should look like this:
www.example.com/products?productID=3
If the productID part is missing, it becomes undefined.

to retrieve a mysql data in php and echo the retrieved data

<?php
$username = "root";
$password = "password";
$database = "xxxxxx";
$link = mysql_connect("localhost", $username, $password);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
mysql_select_db('xxxxxx', $link);
$result = mysql_query($query) or die(mysql_error($link));
$num = mysql_num_rows($result);
mysql_close();
$rows = array();
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
print_r($rows);
?>
This is my code i want to display the roll number of the currently logged in user and
when i run this code i get no database selected.
First of all, you should code properly, means database connection and database selection should be on top:
<?php
$username = "root";
$password = "password";
$database = "xxxx";
$link = mysql_connect("localhost", $username, $password);
mysql_select_db('xxxx', $link);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
$result = mysql_query($query) or die(mysql_error($link));
$num = mysql_num_rows($result);
$rows = array();
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
print_r($rows);
mysql_close();
?>
Also moved mysql_close(); on last
One other main point was, now mysql_ is deprecated, please use mysqli_
Remove this statement from line number 10
mysql_close();
just remove these two lines before while that will solve ur problem
$result = mysql_query($query) or die(mysql_error());
$rows = array();
Use this code and use mysql_close function in the last.
<?php
$username = "root";
$password = "password";
$database = "xxxxxx";
$link = mysql_connect("localhost", $username, $password);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
mysql_select_db($database, $link);
$result = mysql_query($query) or die(mysql_error($link));
$num = mysql_num_rows($result);
$rows = array();
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
print_r($rows);
mysql_close();
?>
You have closed the connection from MySQL before the mysql_query mysql_close();
try this
<?php
$username = "root";
$password = "password";
$database = "dfsdftwsdgdfgdfsgsdf";
$link = mysql_connect("localhost", $username, $password);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
mysql_select_db('xxxxxxx', $link);
$result = mysql_query($query) or die(mysql_error($link));
$num = mysql_num_rows($result);
$rows = array();
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
mysql_close();
print_r($rows);
?>
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
The order should be like this
mysql_select_db('meipolytechnic', $link);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
mysql_select_db('meipolytechnic', $link);

Categories