Update multiple records mysql - php

Hi Guys my code is below.
What I'm trying to do is to update all of the value in mysql that satisfy a specific a specific condition. Now when I just type in the mysql query:
UPDATE `$DBNAME`.`video` SET `Secret_key` = '0'
The query works fine but I can't seem to write php code that will do this.
Any help is welcomed
<?php
// Part 1 (works fine)
include("/home3/kintest2/public_html/include/config.local.php");
$connect= mysql_connect ($DBHOST,$DBUSER,$DBPASSWORD);
$select= mysql_select_db($DBNAME, $connect);
// End of Part 1
// Part 2 (works fine)
$test2= "SELECT * FROM `video`";
$results= mysql_query($test2, $connect);
$num_rows = mysql_num_rows($results);
// End of part 2
// Part 3 ( Not too sure about this part)
for ($num_rows= $count; $count >= 0; $count--)
{
mysql_query("UPDATE `$DBNAME`.`video` SET `Secret_key` = '0'",$connect);
}
// End of part 3 ( Not too sure about this part)
?>

<?php
// Part 1 (works fine)
include("/home3/kintest2/public_html/include/config.local.php");
$connect= mysql_connect ($DBHOST,$DBUSER,$DBPASSWORD);
$select= mysql_select_db($DBNAME, $connect);
// End of Part 1
//no need of part 2 to update records in table
//part 3 loop removed
mysql_query("UPDATE `$DBNAME`.`video` SET `Secret_key` = '0' ",$connect);
?>
Try this. May it will solve ur problem.

For starters I'd take the query out of the loop (and just delete the loop). If it still doesn't work, try hard coding the database name. If that doesn't work, make sure $connect is what you think it is. Look up mysql_query() and make sure you're using it right.
If you try everything above, you'll either find the problem or at least figure out what the problem is not.

Related

Php Array not displaying - limit of MySQL Values

I need to know If php, mysql or even my script has a limit of values or memory, or something that doesn't show me all the values of my database table.
Here is my script:
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "SELECT id_team,name FROM teams ORDER BY id_team LIMIT 50;";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('id_team'=>$row[0],'name'=>$row[1]));
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
If I run this script with the LIMIT 50 condition, it works, but when I change the limit to 70 or more It doesn't work anymore and doesn't print me anything. I don't know if there is like a limit of memory or values or something. Can someone help me please?
This is my output when I use Limite 5 (for example):
{"result":[{"id_team":"3","name":"Tonkas"},{"id_team":"4","name":"Combis"},{"id_team":"5","name":"Looney
Tunes"},{"id_team":"6","name":"Bullets"},{"id_team":"7","name":"Valkiry"}]}
I use a counter inside the loop and It always return me the number of the LIMIT I give in the condition, If I give LIMIT 100, the counter prints me 100, but I don't get anything from the array.
PHP Version 5.6.29 & JSON version 1.2.1 - Hosted in 1and1
SOLVED
It was the codification (charset) when the loop found a invalid character (in this case letter 'ñ') it just break and didn't continue filling the array, just set the charset and worked:
mysqli_set_charset($con,'utf8');

MySQL query's suceed in PHPMyAdmin but not in the PHP Script itself

Just while playing around with Querying in PHP i ran into some trouble. The title of this post explains the problem. When i run a query in PHPMyAdmin the results will be different from the results i get in the PHP program itself. Here is the code i am using. Sorry if it looks a little odd i've been cutting and pasting things all over the place in a frantic attempt to get it working.
$connect = array('username'=>'root', 'host'=>'127.0.0.1', 'password'=>'');
$link = mysql_connect($connect['host'], $connect['username'], $connect['password']) or die('Error creating link: ' . mysql_error());
mysql_select_db('testing_pages', $link) or die('Error connecting to database: ' . mysql_error());
$sql = "SELECT `name` FROM `names`";
$query = mysql_query($sql, $link) or die('Query Failed! Check error:<br/><br/>' . mysql_error());
$query_2 = mysql_fetch_array($query);
$query = $query_2;
$loop = count($query);
$count = 0;
while($count <= $loop) {
echo $query[$count] . '<br/>';
$count++;
}
see, what im trying to get it to do is read all the names, pop it into an array, then print them out with a while loop. But it only seems to return 1 result and thats the first name in the databse. but when i run the EXACT query through phpmyadmin it will return every name in the database... Another odd thing, when using the 'count' function to get the number of values in the array is claims that there are 3 values, but during the loop it just prints out the first name, then for the second two it returns an 'Undefined index'.
Hope i dont seem like a noob right now... And i hope i explained everything well. Thanks to anyone who can help.
mysql_fetch_array fetches one row in the form of an array. Here are the docs.
And pay attention to that big warning message at the top of the page when you read the docs...

Newbie While Loop

I get the following error when i try to display index.php
Warning: mysql_numrows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\cards\index.php on line 18
I can't tell if I'm not connecting to the database correctly or if thre is some other error. Basically I'm trying to display 3 random lines of data from my table "cards". The column in the table that I want to display the data from is "playerName". I'm not worried about formatting the data yet. Code is below:
<?php
include_once 'header.php';
require_once 'config.php';
$con = mysql_pconnect("localhost","*****","*****");
mysql_select_db("USE cards",$con);
$cards=0;
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM cards ORDER BY RAND() LIMIT 3";
$results = mysql_query($sql);
$array = mysql_fetch_array($results);
$num=mysql_num_rows($results);
$i=0;
while ($i < $num) {
echo $rows['playerName'];
$i++;
}
include_once 'footer.php';
?>
I know this is probably a simple newbie question, but I appreciate the help.
Your mysql_select_db call should take only the database name as first argument, not the entire USE statement. Most likely it silently fails and does not select the cards database. Then the query fails (again, silently) returning false.
Also, it's mysql_num_rows (not mysql_numrows).
Manual: http://php.net/manual/en/function.mysql-num-rows.php
Two things, echoing an array wont print it(echo $results;) instead use print_r($results) or var_dump($results) and the other thing, I reckon the error is caused be cause no results are returned, but if you use var_dump or print_r you will be able to verify whether that is the case :)
also there is a typo here: $con = mysql_pconnect("localhost","jalexander","brunswick3"); remove the p from connect :)
Also, you might want to replace your db credentials with asterixes on here :)
Finally, you declare the variable cards but never use it?
In fact one more, change $rows[playerName] to $rows['playerName']
To get different rows instead of using a while loop you can use a foreach like so:
foreach($array as $row){
echo $row['playerName'];
}
Or to do it using a while loop use
$i=0;
while ($i < $num) {
echo $rows[$i]['playerName'];
$i++;
}
It did not work in your current code because you was not looping through the elements of the array, just echoing the same one each time

Inner Join query on 2 different databases - Mysql

We have 2 different databases that I'm trying query against each other with an inner join.
When I run the query from phpmyadmin, the query works perfectly. However, when I attempt to put the query into a php page, I cannot get the line to work. I assume I'm missing something in the mysql_select_db line where I reference the host/db/user/pass for the first database.
What am I missing here to get this query to function on the page? Again, I'm confident the actual query works since it does run in phpmyadmin.
Thanks in advance as always.
Here's the code I'm working with....
$hostname_db = "123.456.78.910";
$database_db = "votes_db";
$username_db = "votes_dbuser";
$password_db = "password123";
$db = mysql_connect($hostname_db, $username_db, $password_db, true) or trigger_error(mysql_error(),E_USER_ERROR);
$hostname_db2 = "123.456.78.910";
$database_db2 = "survey_db";
$username_db2 = "survey_dbuser";
$password_db2 = "password456";
$db2 = mysql_connect($hostname_db2, $username_db2, $password_db2, true);
// trying to make this work, query ok in phpmyadmin, but not on the php page
mysql_select_db($database_db, $db);
$query_testdb3 = sprintf("SELECT votes_db.vote_table.vote_survey_id
FROM votes_db.vote_table
inner join survey_db.survey_table
ON votes_db.vote_table.vote_survey_id = survey_db.survey_table.survey_id
WHERE votes_db.vote_table.vote_survey_id = 1457 ");
$testdb3 = mysql_query($query_testdb3, $db) or die(mysql_error());
$row_testdb3 = mysql_fetch_assoc($testdb3);
$totalRows_testdb3 = mysql_num_rows($testdb3);
Sometime ago I have the same issue (mysql-php multiple databases problem)
So, remove the "true" option on the second ($db2) and everything should be fine.
Gl
$`totalRows_testdb3 = mysql_num_rows($testdb3);`
change the column # in "$testdb3" in the last line to "$row_testdb3".
$totalRows_testdb3 = mysql_num_rows($row_testdb3);
Also if you put each $db and db2 in separate class and function with a:
mysql_close($db);
after each query.
It was a permissions problem. The user that was connecting to the database didn't have the proper privliges. Hosting company fixed this for me, so I can't tell you exactly what they did, but it now works.
Tim was correct, it was working from phpmyadmin since I was logged in as a superuser...

First record not picked up in pagination script (despite the fact it is set to show all records > NOW() as query)

This is my pagination script, which I've been working on:
http://pastebin.com/4mpjdWKD
This is the query page which displays the records - but it omits the first record:
http://pastebin.com/kJZy9fv0
The actual query is this:
<?php
//Require the file that contains the required classes
include("pmcPagination.php");
//PhpMyCoder Paginator
$paginator = new pmcPagination(20, "page");
//Connect to the database
mysql_connect("localhost","root","PASSWORD HIDDEN FOR SECURITY REASONS");
//Select DB
mysql_select_db("housemde");
//Select only results for today and future
$result = mysql_query("SELECT * FROM housemd WHERE expiration > NOW()
order by airdate;");
$recordCount = mysql_num_rows($result);
//You can also add reuslts to paginate here
mysql_data_seek($result,0) ;
while ($row = mysql_fetch_array($result))
{
$paginator->add(new paginationData($row['programme'],
$row['channel'],
$row['airdate'],
$row['expiration'],
$row['episode'],
$row['series'],
$row['epno'],
$row['setreminder']));
}
?>
and the dates are all in the future, yet to even get the first record to show, I have to duplicate it in PhpMyadmin - so what is wrong with my script?
Is it something to do with mysql_fetch_array, and if so, where do I need to fix the script?
In any case, here's the data from the database, all showing records for future events:
http://pastebin.com/gwcv7qza
In short, the basic problem is, I have to manually duplicate a record to get it to show in the script sometimes, and I'm trying to find a fix - but I'm having trouble looking for a solution. I've tried myself, and it didn't work.
I hope I've explained this well enough.
Any help on fixing this is appreciated (just post the solutions in my pastebin links where it allows you to submit a correction/amendment to the pastebin, if you want.)
Thanks!
I think the problem is in your Paginatop class
this row $result = ($page - 1) * $resultsPerPage + 1; will return 1 for the first page and you will show the second element in $this->items[$result] array - $this->items[1], not the first $this->items[0].
Give it a try with $result = ($page - 1) * $resultsPerPage;
for page 1 $result will be 0
for page 2 $result will be 19 (if showing 20 records per page)

Categories