MySQL result in PHP by ID - php

How can i display something out of my databse by an ID number?
I already can get the data out of the database by searching the row 'content' but i want to get it by an ID number.
I have three rows in my database table: ID, pagename and content.
Here is the code that i already have for getting the data:
<?php
$con=mysqli_connect("localhost","jordyyd108_mario","testdb","jordyyd108_mario");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM pages");
while($row = mysqli_fetch_array($result)) {
echo $row['content'] ;
}
mysqli_close($con);
?>
Thanks!

use WHERE clause and pass your id try
mysqli_query($con,"SELECT * FROM pages WHERE ID = '2'");
or pass a variable
mysqli_query($con,"SELECT * FROM pages WHERE ID = '$id'");

You can by using WHERE :-
$result = mysqli_query($con,"SELECT * FROM pages where id = 1");

Related

Use PHP loop to fetch tables data from the table which contain names of database tables

I have table named category which contain names of other tables in the same database. I want to fetch table names from category table and then fetch data from each table from db. So far I have this code below:
$db = new mysqli('localhost', 'root', '', 'db_cat');
if($db){
// $q = "SELECT TABLE";
// $echo = $db->query($q);
// echo $echo;
// $result = $db->query("SHOW TABLES");
$qCat="SELECT * FROM product_category";
$cat_query= $db->query($qCat) or die(mysql_error());
while ($fetch= $cat_query->fetch_object())
{
$cat_id=$fetch->id;
$category=$fetch->category;
$p_cat=str_replace(" ","_",strtolower($category).'_categories');
//if(strlen($category)>22){$fine_product_name= substr($service, 0,19).'...';}else{ $fine_product_name=$category;}
$result = $db->query("SHOW TABLES");
while($row = $result->fetch_array()){
$tables[] = $row[0];
}
}
The second query must be different.
$result = $db->query("SELECT * FROM $category");
while($row = $result->fetch_array()){
$tables[] = $row[0];
}
print_r($tables);
First of all your design to connect to a database is not that good, Please check the below code for a proper way of connecting to it.
<?php
$con=mysqli_connect("localhost","root","","db_cat");
//servername,username,password,dbname
if (mysqli_connect_errno())
{
echo "Failed to connect to MySql: ".mysqli_connect_error();
}
?>
Here is a sample code of getting data from a table ( where this table name is in another table).
$get_table_name ="SELECT TableName FROM table_name";
$get_name=mysqli_query($con,$get_table_name);
$count=0;
while($row_name=mysqli_fetch_array($get_name)){
$count++;
$tbName=$row_name['TableName'];
$_SESSION['table_name'][count]=$tbName;
}
This will show you how to fetch data from one table. You can use a For loop to get all the tables
$table=$_SESSION['table_name'][1];
$get_table ="SELECT * FROM $table";
.... // Normal way of fetching data
You can try to adjust your code according to this and improve it.
For further reference please refer http://php.net/manual/en/book.mysqli.php

Displaying a SQL table data for a specific user

I want to display the table participantes with the columns sorteo, nombre and fecha.
The user info is on another table sellify_users (usern column).
I want to display only that user data using:
SELECT * FROM participantes WHERE nombre = 'usern'
But usern is not in the same table, so if possible I want to call the sellify_users to get the usern data.
<?php
$user = 'database_user';
$password = 'database_pass';
$database="database_name";
mysql_connect(localhost,$user, $password);
#mysql_select_db($database) or die( "Unable to select database");
echo $query = "SELECT * FROM participantes WHERE nombre='usern'";
$result = mysql_query($query);
mysql_close();
?>
If you meant that a user has a record in the table sellify_users and you need to find the usern from it in order to use it in the next query:
$query = "SELECT * FROM participantes WHERE nombre='usern'";
Then all you need to do is to run a query for the table sellify_users first and get the value usern from it, store it in a variable and then use that in your second query. Something like:
$query1 = "SELECT * FROM sellify_users";
$result1 = mysqli_query($con, $query1);
$row1 = mysqli_fetch_assoc($result1);
$usern = $row1['usern'];
$query2 = "SELECT * FROM participantes WHERE nombre='usern'";
$result2 = mysqli_query($con, $query2);
while($row2 = mysqli_fetch_assoc($result2)){
echo $row2['ColumnNameHere1'];
echo $row2['ColumnNameHere2'];
}
Notice: I've passed $con which denotes a connection variable, i.e.
you should read about mysqli or PDO and if there's anything you do not understand, feel free to shoot a query.
EDIT:
If by any chance you are trying to use the last inserted record, you should look for mysqli_insert_id($con); and use that instead.

How to Select Specific Info From MySQL

I'm trying to a number from a specific table, row, and column but can't seem to get it right.
The table is tblproducts. The column is qty. And I need to select where id is 13.
This is the code I'm using but it isn't returning anything:
<?php
$con=mysqli_connect("localhost","blank","blank","blank");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT qty FROM tblproducts");
while($row = mysqli_fetch_array($result))
{
echo $row['13'];
}
mysqli_close($con);
?>
Any help would be appreciated
do it all in the query like so
SELECT qty FROM tblproducts where id =13
no loop needed
$result = mysqli_query($con,"SELECT qty FROM tblproducts where id =13");
$row = mysqli_fetch_assoc($result);
echo $row['qty'];
Your sql syntax is a bit off, see my corrections. Your echo of the row['13'] also looks suspicous. You can use print_r() to print arrays to find which key you need to use. It is probably $row['qty'].
<?php
$con=mysqli_connect("localhost","blank","blank","blank");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT qty FROM tblproducts where id = 13");
while($row = mysqli_fetch_array($result))
{
print_r($row); // You can use print_r to have a look at your full array. The index '13' might not be what you where looking for.
}
mysqli_close($con);
?>

Pull 4 table rows from MySQL table and display with PHP on a page?

I need to simply Pull 4 table rows from MySQL table and display with PHP on a page? Can someone help me with this code... All I have is:
include_once ('db.php');
$link = mysql_connect($db,$user,$pass) or die("Can't connect to Database");
mysql_select_db($table,$link);
/* Main */
$query="SELECT * FROM $subtable ORDER BY id DESC LIMIT 1";
$result=mysql_query($query);
HTML
<?php echo mysql_result('tablerow'); ?>
Here is some code that should get you started!
$link = mysql_connect($host,$user,$pass,$table);
$loop = mysql_query($link,"SELECT * FROM $subtable ORDER BY id DESC LIMIT 4");
while ($row = mysql_fetch_array($loop))
{
echo $row['yourrow']."<br/>";
}
mysql_close($link);
Just replace yourrow with the name of the row that you want to print out! Don't forget to set the variables $host $user $pass and $table

php echo data from certain id

EDIT: I'm sorry I was unclear, I try to explain it right this time.
I have this data in a database table called tMenu:
id page_nl text
1 index_1 index1_text
2 index_2 index2_text
3 index_3 index3_text
These are 3 pages on my website called (in this case) index_1, index_2 and index_3. I have programmed it is such a way that each page shows there index1_text.
What I want now is to show page_nl in a menu. The code I have now is:
$qh = mysql_query('SELECT id, page_nl FROM tMenu ORDER BY id');
$row = mysql_fetch_array($qh);
$id = 'id';
<? echo $row['page_nl']; $id=="1" ;?>
<? echo $row['page_nl']; $id=="2" ;?>
<? echo $row['page_nl'];?>
In the way it is now it shows only page_nl from id 1, but I want that the next link shows page_nl from id 2. I hope my question is more clear now.
Your question isn't very clear - are you asking for something like this
$sql = "select * from yourtable where id = 1";
$result = mysql_query($sql);
//I am assuming there are more than 1 rows for ID 1
while($row = mysql_fetch_assoc($result)) {
echo $row['page_nl'];
}
OR ============================
$sql = "select * from yourtable"; //Select All
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
if($row['id'] == 1)
{
echo $row['page_nl'];
}
}
Presuming you mean database table, you need a routine to connect to the database then fetch the info:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "databasename"); // database name
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM table_name"; // put table name here
$result = $mysqli->query($query);
/* numeric array */
/* associative array */
$row = $result->fetch_array(MYSQLI_ASSOC);
printf ("%s (%s)\n", $row["id"], $row["page_nl"]);
/* free result set */
$result->free();
/* close connection */
$mysqli->close();
?>
You need to use a foreach($var as $key =>$value) loop

Categories