PHP - Mysql Scalar?
Hi All
I've a question regarding php and mysql database interrogation. Is there a way of applying a scalar database call from php, rather than having to return values in an array? For example if I know there is only 1 value in a DB table which matches a select statement, do I have to return it with the following or is there another function which will save me using arrays? Code: [Select] $sql="SELECT name FROM users WHERE id = 10"; $sql=mysql_query($sql); $row=mysql_fetch_array($sql); This would then give me $row['name'] to work with. Is there a way of doing this? Similar TutorialsIn this array, I have 3 broken tools at $10.00 each: [TOOLS] => Array ( [good_quality] => 3 [good_price] => 10.00 [broken] => 3 [broken_price] => 5.00 ) ) foreach($tools as $i => $val) { echo 'We have '.$val["broken"].' broken tools at '.$val["broken_price"].' each"; } Sometimes I have no broken tools, then the array will look like this: [TOOLS] => Array ( [good_quality] => 3 [good_price] => 10.00 ) ) If I were to run the loop now, it would give an "undefined index" error. However, when I define the index like this, it will give a "Cannot use a scalar value as an array" error:
foreach($tools as $i => $val) { Why? And what is the best way to handle this, please? Edited April 11, 2020 by StevenOliverWhen login in (in login.php), the script is assigning user information to scalar variables like so: $user_name = $assoc['user_name']; $user_id = $assoc['user_id']; Now I'd like to make use of those variables on other pages like this for example: echo "Posted by " . $user_name; I want that to be visible for everybody, even if the user logs out. Any ideas? create table mimi (mimiId int(11) not null, mimiBody varchar(255) ); <?php //connecting to database include_once ('conn.php'); $sql ="SELECT mimiId, mimiBody FROM mimi"; $result = mysqli_query($conn, $sql ); $mimi = mysqli_fetch_assoc($result); $mimiId ='<span>No: '.$mimi['mimiId'].'</span>'; $mimiBody ='<p class="leading text-justify">'.$mimi['mimiBody'].'</p>'; ?> //what is next? i want to download pdf or text document after clicking button or link how to do that Hello everyone, Sorry if this has been answered but if it has I can't find it anywhere. So, from the begining then. Lets say I had a member table and in it I wanted to store what their top 3 interests are. Their$ row has all the usual things to identify them userID and password etc.. and I had a further 3 columns which were labled top3_1 top3_2 & top3_3 to put each of their interests in from a post form. If instead I wanted to store this data as a PHP Array instead (using 1 column instead of 3) is there a way to store it as readable data when you open the PHPmyadmin? At the moment all it says is array and when I call it back to the browser (say on a page where they could review and update their interests) it displays 'a' as top3_01 'r' as top3_02 and 'r' as top3_03 (in each putting what would be 'array' as it appears in the table if there were 5 results. Does anyone know what I mean? For example - If we had a form which collected the top 3 interests to put in a table called users, Code: [Select] <form action="back_to_same_page_for_processing.php" method="post" enctype="multipart/form-data"> <input name="top3_01" type="text" value="enter interest number 1 here" /> <input name="top3_02" type="text" value="enter interest number 2 here" /> <input name="top3_03" type="text" value="enter interest number 3 here" /> <input type="submit" name="update_button" value=" Save and Update! " /> </form> // If my quick code example for this form is not correct dont worry its not the point im getting at :) And they put 'bowling' in top3_01, 'running' in top3_02 and 'diving' in top3_03 and we catch that on the same page with some PHP at the top --> Code: [Select] if (isset($_POST)['update_button']) { $top3_01 = $_POST['top3_01']; // i.e, 'bowling' changing POST vars to local vars $top3_02 = $_POST['top3_02']; // i.e, 'running' $top3_03 = $_POST['top3_03']; // i.e, 'diving' With me so far? If I had a table which had 3 columns (1 for each interest) I could put something like - Code: [Select] include('connect_msql.php'); mysql_query("Select * FROM users WHERE id='$id' AND blah blah blah"); mysql_query("UPDATE users SET top3_01='$top3_01', top3_02='$top3_02', top3_03='$top3_03' WHERE id='$id'"); And hopefully if ive got it right, it will put them each in their own little column. Easy enough huh? But heres the thing, I want to put all these into an array to be stored in the 1 column (say called 'top3') and whats more have them clearly readable in PHPmyadmin and editable from there yet still be able to be called back an rendered on page when requested. Continuing the example then, assuming ive changed the table for the 'top3' column instead of individual colums, I could put something like this - Code: [Select] if (isset($_POST)['update_button']) { $top3_01 = $_POST['top3_01']; // i.e, 'bowling' changing POST vars to local vars $top3_02 = $_POST['top3_02']; // i.e, 'running' $top3_03 = $_POST['top3_03']; // i.e, 'diving' $top3_array = array($top3_01,$top3_02,$top3_03); include('connect_msql.php'); mysql_query("UPDATE members SET top3='$top3_array' WHERE id='$id' AND blah blah blah"); But it will appear in the column as 'Array' and when its called for using a query it will render the literal string. a r r in each field instead. Now I know you can use the 'serialize()' & 'unserialize()' funtcions but it makes the entry in the database practically unreadable. Is there a way to make it readable and editable without having to create a content management system? If so please let me know and I'll be your friend forever, lol, ok maybe not but I'd really appreciate the help anyways. The other thing is, If you can do this or something like it, how am I to add entries to that array to go back into the data base? I hope ive explained myself enough here, but if not say so and I'll have another go. Thanks very much people, L-PLate (P.s if I sort this out on my own ill post it all here) I have following piece of code below, and I would like to be able to express it pure SQL, something that could go into a .sql file :
$request_string='SELECT topic_forum_id FROM topic_table WHERE topic_id= 2014'; $query=database->prepare($request_string); $query->execute(); $data=$query->fetch(); $query->closeCursor(); $forum=$data['topic_forum_id']; $request_string='INSERT INTO post_table (post_topic,post_forum) VALUES (2014,:forum)'; $query=database->prepare($request_string); $query->bindValue(':forum',$forum,PDO::PARAM_INT); $query->execute(); $data=$query->fetch(); $query->closeCursor();Is it possible ? So i have this php as shown below. It should make a list of comments with comment replies below their comment respectively. The problem is that it only goes through and shows 1 comment and all the comment replies for that one comment. It should be showing all comments i have in the db for that article. If i remove the second while then it shows all the comments correctly but no comment replies then... How do i get this script to loop through the db for every comment but also loop through every comment reply for that $row[id]? If anyone has a better / more efficient way of what I am trying to do, please explain or show example (i am open to anything)... Code: [Select] // what article are we showing? $article_to_show_id = $_GET['article_id']; $active_is_set_text = "1"; // Active Column text that makes it okay to show // Finding the article $search_for_article = mysql_query("SELECT * FROM articles WHERE id = '$article_to_show_id' AND active = '$active_is_set_text'"); while($row = mysql_fetch_array($search_for_article)) { // format the last updated date right $update_date_edit = $row[update_date]; $update_date_edit = date('F j, Y \a\t h:ia', $update_date_edit); $row[update_date] = $update_date_edit; // format the submit updat date right $submit_date_edit = $row[submit_date]; $submit_date_edit = date('F j, Y \a\t h:ia', $submit_date_edit); $row[submit_date] = $submit_date_edit; echo ' <div> ', $row[title] ,' </div> <div> by: ', $row[author] ,' on ', $row[submit_date] ,' </div> <div> ', $row[content] ,' </div> <div> Last Updated: ', $row[update_date] ,' </div> <form action="article_reply.php" method="post"> <input type="hidden" name="article_id" value="', $row[id] ,'" /> <button name="article_reply" type="submit" value="submit">Reply</button> </form> '; } $comment_count = 0; $comment_reply_count = 0; // Finding all of the comments $search_for_article = mysql_query("SELECT * FROM article_comments WHERE article_id = '$article_to_show_id' AND reply_id = '0'"); while($row_comment = mysql_fetch_array($search_for_article)) { // format the submit updat date right $comment_date_edit = $row_comment[comment_date]; $comment_date_edit = date('F j, Y \a\t h:ia', $comment_date_edit); $row_comment[comment_date] = $comment_date_edit; echo ' <br> <br> COMMENT:<br> <div> By: ', $row_comment[username] ,' on ', $row_comment[comment_date] ,' </div> <div> ', $row_comment[comment] ,' </div> '; $comment_count++; // Finding all of the comment replies if any $search_for_article = mysql_query("SELECT * FROM article_comments WHERE article_id = '$article_to_show_id' AND reply_id = '$row_comment[id]'"); while($row_two = mysql_fetch_array($search_for_article)) { // format the submit updat date right $comment_date_edit = $row_two[comment_date]; $comment_date_edit = date('F j, Y \a\t h:ia', $comment_date_edit); $row_two[comment_date] = $comment_date_edit; echo ' <br> <br> COMMENT REPLY:<br> <div> By: ', $row_two[username] ,' on ', $row_two[comment_date] ,' </div> <div> ', $row_two[comment] ,' </div> '; $comment_reply_count++; } } Need some help I have 2 tables in a database and I need to search the first table and use the results from that search, to search another table, can this be done? and if it can how would you recommend that I go about it? Thanks For Your Help Guys! Hey, I was wondering.. Is there any php functions or any way I can get some information from MySQL? I want to be able to echo out the uptime for MySQL, and some other things.. I've seen it done before so I know its possible lol. Hey guys, I've heard about MySQL being replaced with PDO some time in the future, but as a simple question, I'm coding a personal website for my use only, would I still be able to use MySQL rather than changing it to PDO and learning that?
Its not going to be anything big or nothing just to keep me active in my spare time.
Thanks for you help.
I am having a bit of trouble here. I will explain what I am trying to do: I am trying to pull the Field Name: Account into my input form. My input form code looks like this: Code: [Select] Account Number: <input type="text" value="<?php echo($Account); ?>" name="Account"/> My PHP code above this looks like this: <?php @ $db = mysql_connect("localhost", "usr", "pass"); mysql_select_db("EmployeeInfo"); $sQuery = sprintf("SELECT * FROM EmployeeInfo WHERE EmployeeInfo.Name LIKE '%s'", mysql_real_escape_string($_POST['NameSelect'])); $result = mysql_query($sQuery); $actualResult = mysql_fetch_array($result); echo $actualResult['aSQLRow']; ?> My select query works properly. Can anyone find my error because I sure can't Thanks in advance, dmcdaniel Hi all I am am trying to use the following piece of code to import a CSV file into a mySQL database: Code: [Select] $filename = $_FILES['sel_file']['tmp_name']; $handle = fopen($filename, "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { mysql_query("INSERT INTO user SET id = '".$userid."', name = '".$data[0]."', email = '".$data[1]."', phone = '".$data[2]."'") or die(mysql_error()); } fclose($handle); It works great except it only imports one line in the CSV file. How do I get it to keep adding the lines into the DB? My CSV looks like this: Pete Naylor,test@mail.com,0800101101 Bob Jones,bob@mail.com,08700123123 Many thanks for for help Pete Hi all! I am very new to php and mysql world so I could need some help. I found this UCP php code to control a database, where we could find various userinfo. I edited it and for unknown reasons everything else works except the change password. Here is the NewPassword.php: Code: [Select] <html> <body> <form action="Newpass.php" method="post"> <b><font size="4" color="#000080">Change your password! </font></b> <p>Old password <input type="password" name="P1" size="20" /> </p> <p>New Password <input type="password" name="P2" size="20" /></p> <p>Confirm New Password <input type="password" name="P3" size="20" /></p> <p> <input type="submit" /> <input type="reset" /> </p> <a href="Logged.php"">Go back</a> </form> </body> </html>And here is the Newpass.php Code: [Select] <?php session_start(); $pass1 = $_POST["P1"]; $pass2 = $_POST["P2"]; $pass3 = $_POST["P3"]; $escpass1 = mysql_real_escape_string($pass3); $escpass2 = mysql_real_escape_string($pass3); $escpass3 = mysql_real_escape_string($pass3); $user = $_SESSION['Username']; if(!isset($_SESSION['Username'])) { echo('You are not logged in!'); echo '<a href="index.php"">Go back</a>'; die; } $escuser = mysql_real_escape_string($user); include("database.php"); $query = "SELECT * FROM playerinfo WHERE user = '$escuser'"; $result = mysql_query($query); $row = mysql_fetch_row($result); if (!$con) { die('Could not connect: ' . mysql_error()); } $username_exist = mysql_num_rows($result); if($username_exist == 0) { echo('That username does not exist'); echo '<a href="NewPassword.php"">Go back</a>'; die; } if($escpass1 !== $row[1]) { echo("Wrong old password!"); echo '<a href="New Password.php"">Go back</a>'; die; } if($escpass2 !== $escpass3) { echo("Your new passwords do not match!"); echo '<a href="NewPassword.php"">Go back</a>'; die; } $query = "UPDATE playerinfo SET password = '$escpass3' WHERE user= '$escuser'"; $result = mysql_query($query); echo 'Password Changed!'; $_SESSION['Password'] = $escpass3; echo '<a href="Logged.php"">Go back</a>'; ?>And database.php Code: [Select] <?php $con = mysql_connect("edit","edit","edit"); mysql_select_db("edit"); //Remember that if you are using a external source, change the login info (mysql_connect(server[],user[],pass[]); ?>I edited the user/pw for obvious reasons. So the problem itself: Whatever I insert in password box I get this in return: Code: [Select] Warning: mysql_real_escape_string(): Access denied for user ''@'localhost' (using password: NO) in /home/crpnet/domains/c-rp.net/public_html/blankUCP/Newpass.php on line 6 Warning: mysql_real_escape_string(): A link to the server could not be established in /home/crpnet/domains/c-rp.net/public_html/blankUCP/Newpass.php on line 6 Warning: mysql_real_escape_string(): Access denied for user 'crpnet'@'localhost' (using password: NO) in /home/crpnet/domains/c-rp.net/public_html/blankUCP/Newpass.php on line 7 Warning: mysql_real_escape_string(): A link to the server could not be established in /home/crpnet/domains/c-rp.net/public_html/blankUCP/Newpass.php on line 7 Warning: mysql_real_escape_string(): Access denied for user 'crpnet'@'localhost' (using password: NO) in /home/crpnet/domains/c-rp.net/public_html/blankUCP/Newpass.php on line 8 Warning: mysql_real_escape_string(): A link to the server could not be established in /home/crpnet/domains/c-rp.net/public_html/blankUCP/Newpass.php on line 8 Warning: mysql_real_escape_string(): Access denied for user 'crpnet'@'localhost' (using password: NO) in /home/crpnet/domains/c-rp.net/public_html/blankUCP/Newpass.php on line 16 Warning: mysql_real_escape_string(): A link to the server could not be established in /home/crpnet/domains/c-rp.net/public_html/blankUCP/Newpass.php on line 16 That username does not existGo back Hi im having problems with this can any one point me the right way please Code: [Select] if(isset($_GET['pageID'])){ $id = $_GET['pageID']; } ?> <?php if(isset($_POST['submit'])){ $title = $_POST['title']; $keywords = $_POST['keywords']; $description = $_POST['description']; $menu = $_POST['menu']; $content = $_POST['content']; $query = mysql_query("UPDATE 'page' SET title='$title', keywords='$keywords', description='$description', menu='$menu', content='$content' WHERE 'pageID'= $id"); } ?> I have a CVS file with about 1000 entries which I need to input into a MySQL database. Each line in the file has 5 fields but I am only interested in the first two. These are the description and name. I'm sure I can do this directly with MySQL but I need to use PHP anyway because there is actually other things I need to do with the data. I don't need to go into details because I already have that sorted. I just need to know how to open the CVS file and make a mysql insert loop with the data. Thanks I have this code I want to check to see if the id from table1 is in table2. Can anyone help me with that if($checkProfile) { $sql_chk_tbl_username = "SELECT id FROM members_profile WHERE id = '"$_SESSION['face']"'"; $rs_user_list = mysql_query($sql_chk_tbl_username); if(mysql_num_rows($rs_user_list) <= 0) } ?> Umm.. Yeah, this MySQL line needs some reviewing list($total_banned_servers) = mysql_fetch_row(mysql_query("SELECT COUNT(id) FROM `toplist` WHERE banned='1' && serverowner=". $_SESSION['user'] ."")); Hi im trying to make it get the server from the toplist data by the server owner, after that i want it to check if theres more then 1 server if there is i want it to add all the votes together and if there isnt more then 1 i want it to show the votes <?php $votes = mysql_query("SELECT * FROM toplist WHERE serverowner='{$_SESSION['user']}'") or die(mysql_error()); while($vote = mysql_fetch_array($votes)) { if(mysql_num_rows($vote) > 1 { //gets the server votes and adds them } else { $servervotes = $vote['votes']; } echo $servervotes; ?> } I have been trying very hard to get this script to work but I am no good at arrays... Anyhow, the basic idea is that the script will read an RSS feed and post all that content to the database which will be used to display content on the site. <?php $doc = new DOMDocument(); $doc->load('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/rss.xml'); $arrFeeds = array(); foreach ($doc->getElementsByTagName('item') as $node) { $itemRSS = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue ); array_push($arrFeeds, $itemRSS); } include 'dbconnect.php'; foreach ($arrFeeds as $item) { $sql="INSERT INTO tb_rss (title, desc, link, date) VALUES ($item => title), $item => desc, $item => link, $item => date)"; if (!mysql_query($sql)){ die('Error: ' . mysql_error()); } } ?> Current error messege is: Code: [Select] Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc, link, date) VALUES (Array => title), Array => desc, Array => link, Array =' at line 1 Help please? I am having a problem with my PHP/MySQL coding and I can not figure out what I am doing wrong. I have two tables in a database. One called EmployeeInfo and another called Absence. In the Employee Info table, I have two employees, Derek and Adrian. In the Absence table, I have two records, One for Derek and Adrian. Goal: Using PHP/MySQL, I want to call upon an employee using a form and display that person's absence. The form pulls up correctly. If I do NOT put a WHERE clause into my SQL statement, it pulls up all the absences perfectly fine. Once I put in the WHERE = EmployeeInfo.Name = 'NameSelect' then it fails every time. Here is my SQL Coding: $result = mysql_query("SELECT * FROM Absence LEFT JOIN EmployeeInfo ON(Absence.Account = EmployeeInfo.Account) WHERE EmployeeInfo.Name='NameSelect'"); Any help is greatly appreciated. |