Select a string from mysql table in php doesn't show output - php

I'm trying to extract a string field, under a column called "whenadded", of a mysql table (called "values") with a php script. I'm using this code:
<?php
include("common.php");
$link=dbConnect();
$name = safe($_POST['name']);
$mysqldate = mysql_query("SELECT `whenadded` FROM `values` WHERE `name`= `$name`");
?>
In this table I have only two columns: name and whenadded. Both are strings.
If I try to display the result of $mysqldate, I don't see anything (white field).
[SOLVED] At the end I solved using this:
include("common.php");
$link=dbConnect();
$name = safe($_POST['name']);
$query = mysql_query("SELECT * FROM $dbName . `valorigps` WHERE name = '$name'");
while($row = mysql_fetch_array($query))
{
echo $row['whenadded'];
}

your query string shoud be this:
$mysqldate = mysql_query("SELECT whenadded FROM values WHERE name= '$name'");
or
$mysqldate = mysql_query("SELECT whenadded FROM values WHERE name= '{$name}'");
no need of quote for table columns!

Related

Convert RSS pubdate to timestamp in mysql database

My table feeds in mysql has a RSS timestring column named pubDate.
I would like to add an additional column pubDate_Date and insert the RSS timestring as a real date.
I have created the additional column formatted as DATE and try to use the following code to update the new column with data, but somehow I cannot get it working. Am a newbie here.
function pubDateToMySql($str){
return date('Y-m-d H:i:s', strtotime($str));
};
$sqlCommand = "SELECT * FROM feeds ORDER BY id ASC";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$id = $row["id"];
$pubDate = $row["pubDate"];
$pubDate_Date = pubDateToMySql($pubDate);
$sql = mysqli_query($myConnection, "UPDATE feeds SET pubDate_Date =
$pubDate_Date WHERE ID = $id");
}
mysqli_free_result($query);
To complete this post, this is how I ended up doing it:
UPDATE feeds SET pubDate_Date = STR_TO_DATE(pubDate, '%a, %d %b %Y %T')
In this SQL query
UPDATE feeds
SET pubDate_Date = $pubDate_Date
WHERE ID = $id
you must use date value in ' like this:
UPDATE feeds
SET pubDate_Date = '$pubDate_Date'
WHERE ID = $id
also check type of column pubDate_Date in database

No data displayed by given a specific name and date

This is my table
and now I want to retrieve the data from MySQL to android based on name (Test) and month (01), but no data get displayed. Is there any mistake in my php ?
My php code
<?php
define('HOST','127.0.0.1:3307');
define('USER','root');
define('PASS','');
define('DB','androiddb');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('unable to connect');
$name = $_GET['name'];
$month = $_GET['month'];
$sql = "select * from information WHERE name= '". $name."' and month = '".$month."'";
$res = mysqli_query($con,$sql);
$result=array();
while($row=mysqli_fetch_array($res)){
array_push($result,array('id'=>$row[0],'name'=>$row[1],'weather'=>$row[2],'date'=>$row[3],'status'=>$row[4],
'time_in'=>$row[5], 'time_out'=>$row[6]));
}
echo (json_encode(array("result"=>$result)));
mysqli_close($con);
?>
You have a condition month = ... in your SQL statement but the table doesn't have a month column.
This should work:
$sql = "select * from information WHERE name= '". $name."' and MONTH(date) = '".$month."'";

php: (newbie) database field definition into array

i'd like to write a function which grabs all fields from a database table and puts it in an array.
getting the database fields already works like this:
$sq = "select column_name,data_type from information_schema.columns
where table_name='users'";
i'm just not sure how to store them properly.
when having eg. this database structure:
fieldname type length
--------------------------------
id int 11
username varchar 50
pass varchar 50
lastlogin date
what's the best way to store this data in php in order querying it like this:
$field = myFields["username"];
echo $field->id."/".$field->type."/".field->length;
as you can see i'd like to access the data directly by field name
Make sure you connect to the database first. Then do:
$r = mysql_query('SELECT COLUMN_NAME AS fieldname,
DATA_TYPE AS type,
CHARACTER_MAXIMUM_LENGTH AS length
FROM information_schema.columns
WHERE table_name = "users" AND column_name = "username"');
if ($r)
{
$field = mysql_fetch_object($r);
// $field->type, $field->length, etc.
}
$sql = "YOUR QUERY HERE";
$res = mysql_query($sql);
while ($rowobj = mysql_fetch_object($res)) {
// do what you want to do with $rowobj
echo $rowobj->column_name1 . "<br />\n";
echo $rowobj->column_name2 . "<br />\n";
// ...rest of code...
}
Put your query into $sql variable and replace column_nameX in while loop with columns you SELECTed with your query.
$query = mysql_query("SELECT * FROM table");
$result = array();
while($row = mysql_fetch_array($query))
{
$username = $row['username'];
unset($row['username']);
$result[$username] = (object)$row;
}
Then you can access it by using:
$result['username']->id

what is wrong with this code?

I need to read a text file, query the database table with that name, and store that table's data in another table. So far I have written this code but I don't know why it's not working.
foreach ($lindb as $namedb) {
$query = "SELECT * FROM ntable WHERE name =" .$namedb. "";
$result = mysql_query($query);
while ($r = mysql_fetch_array($result)) {
$query = "INSERT INTO ndtable (name,details,address,login,country) VALUES (\"".$r["name"]."\", \"".$r["details"]."\", \"".$r["address"]."\", \"".$r["login"]."\", \"".$r["country"]."\")";
mysql_query($query);
}
}
You don't have quotes around $namedb
ie. SELECT * FROM ntable WHERE name =" .$namedb. ""; should be SELECT * FROM ntable WHERE name ='" .$namedb. "'";
I suggest a SELECT INTO would be the better choice... and please post the error so we are able to help...

Created a new variable by pulling a value from a MySQL table

I am using a page where a variable $submissionid is being posted to it. I would like to use this variable and pull the field subcheck from a MySQL table called submission. I would like the value of the field subcheck to simply be a new variable $subcheck. I'm not sure how to do this. I have a query below, but how to I convert the result of the query below into a variable called $subcheck? (For any given submissionid, there will only be one $subcheck.)
Thanks in advance,
John
$querysub = mysql_query("SELECT subcheck FROM submission WHERE submissionid = '$submissionid' ");
mysql_query($querysub) or die(mysql_error());
You can try:
$querysub = mysql_query("SELECT subcheck FROM submission WHERE submissionid = ".
mysql_real_escape_string($submissionid));
$result = mysql_query($querysub);
if (!$result) {
die 'Could not run query: ' . mysql_error();
}
$subcheck = mysql_result($result, 0);
This is more of a 'php' question, than it is for mysql.
Look up the 'extract' keyword for PHP Link. Effectively 'extract' takes the contents of an associative array and creates php variables (symbol table entries) using the names of keys. Each php variable will then contain the associated value.
You should be able to just:
$result = mysql_query("SELECT * FROM table");
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
extract( $row ); // Create php variables, named after each column in the table.
$row["field"] == $field; // Will be a true statement after 'extract()'
Enjoy, you now have the ability to have your code dynamic adjust to a DB schema that could be changed.
-- J Jorgenson --
This should work:
$querysub = mysql_query("SELECT subcheck FROM submission WHERE submissionid = '" . $submissionid ."' ");
$result = mysql_query($querysub) or die(mysql_error());
$row = mysql_fetch_assoc( $result );
if ($row ) {
$subcheck = $row['subcheck'];
} else {
echo "Subcheck not found";
}
Be careful with the escape characters around $submissionid in your query string. In your sample, they are probably letting the name of the variable go into the string you send to the mysql server.

Categories