I am storing MySQL Query value in PHP variable, but its not displaying data. P.S: Data is available in MySQL table column.
<?php
$cmsca= mysql_query("SELECT SUM(qa_effort) FROM tbl_uat WHERE product='CAP'");
while ($cresulta = mysql_fetch_array ($cmsca))
$arra[0] = $cresulta[0];
echo $arra[0];
?>
I am out of clues, what is wrong in above code? Need help!
Regards
try this
<?php
$cmsca= mysql_query("SELECT SUM(qa_effort) as sums FROM tbl_uat WHERE product='CAP'");
while ($cresulta = mysql_fetch_array($cmsca))
{
echo $cresulta['sums'];
}
?>
first of all, do not use mysql_query, - it's deprecated, use http://www.php.net/manual/en/mysqli.query.php instead.
Next, you need to connect to the db, before running query;
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$result = mysqli->query("SELECT SUM(qa_effort) as sums FROM tbl_uat WHERE product='CAP'");
while ($row = $result->fetch_array()) {
var_dump($row);
}
$mysqli->close();
?>
How about trying this:
<?php
$arra = array();
$cmsca= mysql_query("SELECT SUM(qa_effort) FROM tbl_uat WHERE product='CAP'");
while ($row = mysql_fetch_array ($cmsca))
$arra = $row;
print_r($arra);
?>
Related
i have SQL query :
SELECT countryCode FROM itins_countries WHERE (itinID = 5);
$countriesIndex = mysql_fetch_array($countriesQuery);
now, in another art of my code I would like to run on the "$countriesIndex" and print all the values it contain ("countryCode");
how can i do that?
while($row = mysql_fetch_array($countriesQuery)){
echo $row['column_name'];
///same for other columns
}
you can use the while loop to loop until all the array element has been printed
try this....
echo "<pre>";print_r($countriesIndex);
mysql_connect is deprecated...you can use mysqli_connect.
$mysqli = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno($mysqli)) {
trigger_error('Database connection failed: ' . mysqli_connect_error(), E_USER_ERROR);
}
mysqli_set_charset($mysqli, "utf8");
$sql = "SELECT countryCode FROM itins_countries WHERE (itinID = 5)";
$result = mysqli_query($mysqli, $sql);
$countries = [];
while($row = $result->fetch_assoc())
{
$users_arr[] = $row;
}
$result->close();
print_r($users_arr);
I cannot connect to my Data Base using this way:
mysql_connect("localhost","root","") or die("1");
mysql_select_db("database") or die("2");
After the connection to the data base I would like to create a while loop with the elements of the data base, this is my code:
$carlistreq = mysql_query("SELECT * FROM cars WHERE brand="'.$_GET['marq'].'"") or die("3");
$count = mysql_num_rows($carlistreq);
if($count==0)
{
echo "No elements";
}
else
{
while($row= mysql_fetch_array($query))
{
//my code
}
}
The issue comes from my SQL attributes like:
mysql_connect
mysql_fetch_array
mysql_select_db
For example.
Here is the error:
Deprecated: mysql_connect(): The mysql extension is deprecated and
will be removed in the future: use mysqli or PDO instead in C:[....]
on line 2
use the mysqli-extension like this:
$mysqli = new mysqli("localhost","user","password","database") or die("1");
$sql = "SELECT * FROM cars WHERE brand='".$_GET['marq']."'";
$result = $mysqli->query($sql);
if($result){
while($row = $result->fetch_assoc()){
//do something with your $row here
}
}
http://php.net/manual/en/book.mysqli.php
Use PDO
$db = new PDO('mysql:host=localhost;dbname=databaseName', 'User' , 'password');
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
Yes now servers are going to handel mysqli and PDO. Try implementing this code:
<?php
$con = mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
// Change database to "test"
mysqli_select_db($con,"test");
// ...some PHP code for database "test"...
mysqli_close($con);
?>
You can user http://php.net/manual/en/book.mysqli.php as reference to understand better how mysqli works.
I'm trying to put through a basic search through my Database based on a user entry, and I could not get it to work, so I'm doing a hard coded search just so I can get it to work... and I can't. My code is as follows:
<?php
$db_connection = mysqli_connect("HOST", "USERNAME", "PASSWORD", "DATABASE");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$data = mysql_query($db_connection, "SELECT * FROM card_collection WHERE ID='1207409700'");
while( $card = mysql_fetch_array( $data ) ) {
echo $card['Name'], '<br />';
}
mysql_close();
?>
I've double checked spelling for everything linked to the database. The ID I'm basing my search from is the first item in my database.
You are using the mysqli_connect to database and then you are using the mysql_query to execute the query it should be mysqli_query
You code should be:
$data = mysqli_query($db_connection, "SELECT * FROM card_collection WHERE ID='1207409700'");
while( $card = mysqli_fetch_array( $data ) ) {
echo $card['Name'], '<br />';
}
mysqli_close();
Replace the mysql with the mysqli. This may be your problem.
I personally use PDO and not mysqli, but is the problem perhaps that you use mysql_query and mysql_fetch_array, instead of mysqli_query and mysqli_fetch_array? Note the missing i
Edit: dangit, beat to the punch :)
<?php
$db_connect = mysqli_connect("host", "user", "pass", "db");
if($db_connect){
$data = mysqli_query($db_connection, "SELECT * FROM card_collection WHERE ID='1207409700'");}
else {"Not Connected to db";}
while( $card = mysqli_fetch_array( $data ) ) {
echo $card['Name'], '<br />';
}
mysqli_close();
?>
I am learning mysqli.
I am trying to fetch data from a table "tbllogin".
//DATABASE CONNECTION
$hostname="p:localhost";
$database="dbLogin";
$username="user1";
$password="pwd1";
$mysqli = new mysqli($hostname, $username, $password,$database);
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
// Create Query
$query = "SELECT * FROM tbllogin";
// Escape Query
$query = $mysqli->real_escape_string($query);
echo $query;
// Execute Query
if($result = $mysqli->query($query)){
print_r($result);
while($row = $mysqli->fetch_object($result)){
echo $row->column;
}
//Free result set
$result->close();
}
?>
But $mysqli->fetch_object($result) is not working. The if statement containing $mysqli->fetch_object($result) does not execute. I cannot identify if there is any error.
Please help.
Also, suggest whether mysqli procedural form is better or object-oriented form?
Thanks in advance.
Shouldn't that be $result->fetch_object() ?
http://php.net/manual/en/mysqli-result.fetch-object.php
From Example 1:
if ($result = $mysqli->query($query)) {
/* fetch object array */
while ($obj = $result->fetch_object()) {
printf ("%s (%s)\n", $obj->Name, $obj->CountryCode);
}
According to the answers on this stackoverflow page, there is not much of a difference between the two.
Try this:
if($result = $mysqli->query($query)){
print_r($result);
while($row = $result->fetch_object($result)){
//do something
}
}
You need to chain the commands together.
Can any one give
The Example php code for connecting and getting a sql stored proceedure
what do you prefer to use? Here is an example taken from php.net:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "CALL get_items(1, #param1, #param2); ";
/* execute multi query */
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}
/* close connection */
$mysqli->close();
remember, that you have to free the resultset, if you do not, you will get an error while executing a next query.
Before I know something about mysqli, I apply mysqli to handle sp's. Just take a look at the follwing example:
$rs = mysql_query("CALL get_items(1, #param1, #param2); ");
$rs = mysql_query("SELECT #param1, #param2" );
while($row = mysql_fetch_assoc($rs))
{
print_r($row);
}
Calling a Stored procedure with PDO