Looking to select multiple values from the database and echo with PHP. (Newbie)
For instance:
SELECT sponser, contract, script FROM Copy WHERE day = '11092014' and time = 4
SELECT sponser, contract, script FROM Copy WHERE day = '11092014' and time = 5
SELECT sponser, contract, script FROM Copy WHERE day = '11092014' and time = 6
How would I set the variables.. something along the lines of this using MYSQLi for multiple variables?
$sqlStremail = "SELECT subcheckr
FROM login
WHERE username = '$u'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$variable = $row["subcheckr"];
Truly appreciate any help.
Yes. Though if your query returns multiple rows, you'll need to use:
while ($row = mysqli_fetch_assoc($result))
{
//do something here
}
you need to make a loop for , foreach or a while loop
ex
while ($row = mysqli_fetch_assoc($result))
{
}
This should do it. First you need to connect, then you build your query. If query fails display an error so you know what went wrong. Then build your data array and use it.
$db = mysql_connect("localhost", "mysql_user", "mysql_password");
$sqlStremail = "SELECT `subcheckr`
FROM `login`
WHERE `username` = '".$u."'"; //needs to be concatenated
$result = mysql_query($sqlStremail, $db);
if(!$result) {
echo "query failed:". mysql_error();
exit;
}
$data = array();
while ($row = mysql_fetch_assoc($result)) {
$data = $row;
}
echo $data['sponsor'];
echo $data['contact'];
echo $data['script'];
//etc
Related
Hey guys i have microsoft sql management studio 18, where I have a database. I'm doing a select statement through php like this:
$conn = OpenCon();
$query = "SELECT id, name, picture, description, numberOfEngines FROM planes";
$result = sqlsrv_query($conn, $query);
if ($result === false) {
$status['status'] = "0";
echo json_encode($status);
}
else{
while($row = sqlsrv_fetch_array($result)) {
$theRows[] = $row;
}
echo json_encode($theRows);
}
CloseCon($conn);
and this is the output:
[{"0":1,"id":1,"1":"114","name":"114","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C525","description":"Cessna C525","4":1,"numberOfEngines":1},
{"0":2,"id":2,"1":"115","name":"115","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C525","description":"Cessna C525","4":1,"numberOfEngines":1},
{"0":3,"id":3,"1":"124","name":"124","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C208B","description":"Cessna C208B","4":1,"numberOfEngines":1},
{"0":4,"id":4,"1":"125","name":"125","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C208B","description":"Cessna C208B","4":1,"numberOfEngines":1}]
How can i remove that leading these duplicates that are showing up twice like the "0":1, or the ariplane.png.
So my output will be like this:
[{"id":1, "name":"114", "picture":"airplane1.png", "description":"Cessna C525", "numberOfEngines":1}]
To return only associative keys in your array, pass SQLSRV_FETCH_ASSOC as the fetchType parameter to sqlsrv_fetch_array:
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
This will give you an array with entries like:
{"id":1,"name":"114","picture":"airplane1.png","description":"Cessna C525","numberOfEngines":1}
If you really want the other numeric keys, keep your code as is and add
unset($row[0])
before
$theRows[] = $row;
I am trying to query a db for an entire column of data, but can't seem to get back more than the first row.
What I have so far is:
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
if($row = mysqli_fetch_array($medicationItemObj, MYSQLI_NUM)){
echo count($row);
}
It's not my intention to just get the number of rows, I just have that there to see how many it was returning and it kept spitting out 1.
When I run the sql at cmd line I get back the full result. 6 items from 6 individual rows. Is mysqli_fetch_array() not designed to do this?
Well, I had a hard time understanding your question but i guess you are looking for this.
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
if($row = mysqli_num_rows($medicationItemObj))
{
echo $row;
}
Or
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
$i = 0;
while ($row = mysqli_fetch_array($medicationItemObj))
{
$medicationItem[] = $row[0];
$i++;
}
echo "Number of Rows: " . $i;
If you just want the number of rows i would suggest using the first method.
http://php.net/manual/en/mysqli-result.num-rows.php
You can wrote your code like below
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
while ($row = mysqli_fetch_assoc($medicationItemObj))
{
echo $row['medication'];
}
I think this you want
You could give this a try:
$results = mysqli_fetch_all($medicationItemObj, MYSQLI_NUM);
First, I would use the object oriented version of this and always use prepared statements!
//prepare SELECT statement
$medicationItemSQL=$connection->prepare("SELECT medication FROM medication");
// execute statement
$medicationItemSQL->execute();
//bind results to a variable
$medicationItemSQL->bind_result($medication);
//fetch data
$medicationItemSQL->fetch();
//close statement
$medicationItemSQL->close();
You can use mysqli_fetch_assoc() as below.
while ($row = mysqli_fetch_assoc($medicationItemObj)) {
echo $row['medication'];
}
please take a look at this code :
$sql = "SELECT * FROM shop";
$result = mysql_query($sql);
echo $result;
echo "before lop";
while ($xxx = mysql_fetch_assoc($result)) {
echo "inside lop";
echo $xxx['column_name'];
}
echo "after lop";
When I run such code i receive :
Resource id #244
before lop
after lop
It did not enter while lop, and I really don't know why :(
I used before such code and there were no problems.
Can someone help me?
$sql = "SELECT * FROM shop";
$result = mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($result);
Check how many records are present in your shop table. I think shop table is empty.That is why not entering in the while loop.
You can do like this
$count = mysql_num_rows($result);
if($count > 0) {
while ($xxx = mysql_fetch_assoc($result)) {
echo $xxx['column_name'];
}
}
I would guess that the call to mysql_fetch_assoc() has returned false, possibly due to no results being returned from the database, this would cause the while loop to not execute even once. I would check the output of var_dump(mysql_fetch_assoc($result)) to ensure that data has been returned.
I have a working code
<?php
$con = mysql_connect('localhost','test','test');
mysql_select_db('test',$con);
require_once("xml2json.php");
$testXmlFile = 'myxml.xml';
$xmlStringContents = file_get_contents($testXmlFile);
$jsonContents = "";
$jsonContents = xml2json::transformXmlStringToJson($xmlStringContents);
$obj =json_decode($jsonContents);
$rows = array();
foreach($obj->rss->channel->item as $item) {
$rows[] = "('".mysql_real_escape_string($item->title)."','".mysql_real_escape_string($item->description)."')";
}
$del_horoscope = "delete from jos_horoscope";
mysql_query($del_horoscope);
$query = "INSERT INTO `jos_horoscope` (`title`,`description`) VALUES ".implode(', ',$rows);
print_r ($query);
mysql_query($query);
// Do the query - do NOT show the output of mysql_error() in a production environment!
if (!mysql_query($query)) echo 'Oh no! Something went wrong with the query: '.mysql_error();
?>
But data is inserting twice with every run of this script. I am using delete statement to delete the data first before inserting new one.Can anyone please help me solve this twice problem. Here is teh screenshot of twice Data filled .http://i.imgur.com/7IM1mOE.png?1
NEW EDIT
Here is the INSERT QUERY echo http://pastebin.com/btZ7f85a
The data is not duplicate.
Thanks in advance
This is untested, but try something like this. I changed the delete to TRUNCATE which will drop all data in the table. I also changed the way the INSERTs work rather than 1 long ass insert I changed it to multiple inserts. There shouldn't be a huge performance hit. Give it a try, let me know.
<?php
$con = mysql_connect('localhost','test','test');
mysql_select_db('test',$con);
require_once("xml2json.php");
$testXmlFile = 'myxml.xml';
$xmlStringContents = file_get_contents($testXmlFile);
$jsonContents = xml2json::transformXmlStringToJson($xmlStringContents);
$obj =json_decode($jsonContents);
$rows = array();
foreach($obj->rss->channel->item as $item)
{
$rows[] = "('".mysql_real_escape_string($item->title)."','".mysql_real_escape_string($item->description)."')";
}
// Remove Data in Table.
$truncate = mysql_query("TRUNCATE TABLE jos_horoscope");
foreach($rows as $row)
{
$query = "INSERT INTO `jos_horoscope` (`title`,`description`) VALUES $row";
$result = mysql_query($query) or die(mysql_error());
}
?>
I looked for an answer prior to writing this but found nothing, but feel free to post a link if I missed it.
I am trying to get the values from a single row of my mysql table. The query I'm using below returns nothing; the echo of mysql_num_rows is always zero. I know the title variable used in WHERE is valid and the database is connected, etc.. Thanks in advanced.
$title = $_REQUEST["title"];
$query = mysql_query("SELECT * FROM links WHERE title = '$title'");
if(!$query) {
die ("Error: " . mysql_error());
}
echo mysql_num_rows($query);
$row = mysql_fetch_row($query);//also tried mysql_fetch_array
$link = $row['link'];
$type = $row['type'];
$user = $row['user'];
$date = $row['date'];
$rating = $row['rating'];
$info = $row['info'];
Try with:
$title = mysql_real_escape_string($_REQUEST["title"]);
$query = mysql_query("SELECT * FROM links WHERE title LIKE '$title'");
echo $query // run it in mysql prompt to check if there are any results first.
if(!$query) {
die ("Error: " . mysql_error());
}
echo mysql_num_rows($query);
$row = mysql_fetch_row($query);//also tried mysql_fetch_array
$link = $row['link'];
$type = $row['type'];
$user = $row['user'];
$date = $row['date'];
$rating = $row['rating'];
$info = $row['info'];
$title = $_REQUEST["title"];
$query = mysql_query("SELECT * FROM links WHERE title = '$title'");
if(!$query) {
die ("Error: " . mysql_error());
}
echo mysql_num_rows($query);
while($row = mysql_fetch_row($query))
{
$link = $row['link'];
$type = $row['type'];
$user = $row['user'];
$date = $row['date'];
$rating = $row['rating'];
$info = $row['info'];
}
If mysql_num_rows returns zero it means that your query is probably not selecting anything from the database. You have to check if your sql query is actualy selecting anything. Check the value of the $title variable and try excecuting the query directly to your database.
You are also vulnerable to sql injections. Always filter user input. You can use mysql_real_escape_string and htmlspecialchars for that.
Try getting values without a where just like this one:
$query = mysql_query("SELECT * FROM links") or die(mysql_error());
If there is wrong it should prompt you the error.
Many people are already saying it but try to cleanse the request parameters ^_^
One common thing to do is print out your sql query to make sure its what you expect.
$sqlquery = "SELECT * FROM links WHERE title='".$title."'";
$query = mysql_query($sqlquery);
echo $sqlquery;
also, as has already been mentioned... you're vulnerable to SQL Injection attacks.
you should use
while($row = mysql_fetch_row($query){...} and also use mysql_real_escape_string to avoid sql injection. And also dont use echo command infront of mysql_num_rows. And even i think its not necessary to get number of rows to execute the remaining part of the script.
P.s i'm on mobile so i'm not able to post script.