PHP - Variable In Database Result?
Hello Guys,
I have a column named message_status it can only have the values "read" or "unread" in it. I would like to show an image in the column next to it. If the message is "read" then I would like to display a green dot, and if it is unread I would like to display a red dot. I would also like to be able to have anything in the row that's "unread" to appear in bold. I completely stumped by this! I have written the following code, which doesn't seem to work, presumably because it is run after the table has been displayed? I am very new to this so please be gentle! Regards, AJLX $result = mysql_query("SELECT * FROM messages where username ='$username'ORDER BY {$_GET['orderbycol']} $sort"); echo "<table border='1'> <tr width='200'> <th>View</th> // code to display table removed </tr>"; $green_dot = $message_status; $red_dot = $message_status; $green = ""; $red = ""; $bold = ""; while ($row = mysql_fetch_array($result)) { echo "<tr>"; $id = $row['ID']; echo "<td><a href='/view_message.php/?view_message=$id'>View</a></td>"; echo "<td>" .$green . $red."</td>"; echo "<td>" .$bold . $message_status = $row['message_status'] . "</td>"; echo "<td>" .$bold. $row['date_sent'] . "</td>"; echo "<td>" .$bold. $row['contact_name'] . "</td>"; echo "<td>" .$bold. $row['subject'] . "</td>"; echo "</tr>"; } if ($green_dot = 'read'){ $green ='<img src=\"assets/red.jpg\"/>'; echo $green; } else { $red ='<img src=\"assets/red.jpg\"/>'; $bold = "<b>"; echo $red; Similar Tutorialshello everyone, I am about to start coding my pages to display results from a database but before i do i want to know information about the following : Is it best to upload images to a database?and display them accordingly? or is it best to use images from a directory? What is most commonly used and or more reliable? Another topic i have trouble finding information on is actually positioning the output from you mysql database, is this practice done with tables?fields and rows? What is this method called? And is there more then one way to go about controlling result layout on your page? Sorry about the 1001 questions , but i am unable to find a clear answer on the topic ..especially question two . Thanks in advance. Hello, So I have a content management site set up, but I'm stuck on one part. I have a query. Code: [Select] Select `email` FROM author WHERE author.id IN(SELECT authorid FROM job WHERE id = '$_POST[id]') I'm a beginner in PHP and SQL. Basically I need the query to run, it will come out with one result and I need that to be where the email is sent to. This is the existing code I have for sending out the email $subject = " Job Application Confirmation"; $message = "Hello $_POST[name] , \r\rYou, or someone using your email address, has applied for the job: $text Using the Resource Locator at Prahan.com. Keep this as future reference. \r\r Full Name: $_POST[name] \r Location: $_POST[location] \r Email: $_POST[email] \r Additional Information: $_POST[info] \r Job ID: $id \r Job Name: $text \r \r Expect a response shortly. \r\r Regards, Prahan.com Team"; $headers = 'From: Prahan.com Team <noreply@prahan.com>'; 'Reply-To: noreply@prahan.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); $to1 = 'pratik@prahan.com'; mail($to1, $result, $message, $headers); Thanks in Advance! I'm writing a highscore board for a game and I can only display one value from a DB... I don't know what I could be doing wrong. I've done this kind of DB work before. <?php $name = $_GET['lvl']; if($_GET['lvl']=="Strength"){ //1/21 $skill = "strlvl"; $exp = "strexp"; } else if($_GET['lvl']=="Attack"){ //2/21 $skill = "atklvl"; $exp = "atkexp"; } else if($_GET['lvl']=="Defence"){ //3/21 $skill = "deflvl"; $exp = "defexp"; } else if($_GET['lvl']=="Hitpoints"){ //4/21 $skill = "hplvl"; $exp = "hpexp"; } else if($_GET['lvl']=="Range"){ //5/21 $skill = "rglvl"; $exp = "rgexp"; } else if($_GET['lvl']=="Magic"){ //6/21 $skill = "mglvl"; $exp = "mgexp"; } else if($_GET['lvl']=="Hitpoints"){ //7/21 $skill = "hplvl"; $exp = "hpexp"; } else if($_GET['lvl']=="Prayer"){ //8/21 $skill = "prlvl"; $exp = "prexp"; } else if($_GET['lvl']=="Runecraft"){ //9/21 $skill = "rclvl"; $exp = "rcexp"; } else if($_GET['lvl']=="Slayer"){ //10/21 $skill = "sllvl"; $exp = "slexp"; } else if($_GET['lvl']=="Thieve"){ //11/21 $skill = "thlvl"; $exp = "thexp"; } else if($_GET['lvl']=="Agility"){ //12/21 $skill = "hplvl"; $exp = "hpexp"; } else if($_GET['lvl']=="Firemaking"){ //13/21 $skill = "fmlvl"; $exp = "fmexp"; } else if($_GET['lvl']=="Woodcut"){ //14/21 $skill = "wclvl"; $exp = "wcexp"; } else if($_GET['lvl']=="Cooking"){ //15/21 $skill = "cklvl"; $exp = "ckexp"; } else if($_GET['lvl']=="Herblore"){ //16/21 $skill = "hblvl"; $exp = "hbexp"; } else if($_GET['lvl']=="Mining"){ //17/21 $skill = "mnlvl"; $exp = "mnexp"; } else if($_GET['lvl']=="Farming"){ //18/21 $skill = "frmlvl"; $exp = "frmexp"; } else if($_GET['lvl']=="Fishing"){ //19/21 $skill = "fshlvl"; $exp = "fshexp"; } else if($_GET['lvl']=="Smithing"){ //20/21 $skill = "smlvl"; $exp = "smexp"; } else if($_GET['lvl']=="Fletching"){ //21/21 $skill = "fltlvl"; $exp = "fltexp"; } else if($_GET['lvl']==""){ //0/21 echo "<center><br /><br />No skill selected!</center><br />"; } mysql_connect("mysql", "15557_test", "**************") or die("Could not connect: " . mysql_error()); mysql_select_db("15557_test"); //STARTS HERE $result = mysql_query("SELECT playerName, $skill, $exp FROM skills ORDER BY $exp ASC LIMIT 5000"); while ($row = mysql_fetch_array($result, MYSQL_NUM)) { echo"Skill: " . $name . "<br />"; printf("Player: %s - Rank: %s - EXP: %s<br />", $row[0],$row[1],$row[2]); mysql_free_result($result //ENDS HERE } ?> I want to save the results of a loop as a variable ie $output. I have tried encasing the php within quotes but it does not work. Is there a way to save the complete results as a variable? $result = mysql_query("SELECT * FROM $table2 WHERE $db_item_1 OR $db_item_2", $connection); if (!mysql_num_rows($result)) { echo "Error 13424 - not working"; exit(); } while ($row = mysql_fetch_array($result)) { echo "<tr><td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\" style=\"font-size: 20px;\">" . $row['date'] . "</div></td> <td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\"></div></td> <td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\" style=\"font-size: 20px;\">" . $row['title'] . "</div></td> <td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\"></div></td> <td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\" style=\"font-size: 20px;\">" . $row['cost'] . "</div></td> </tr>"; }echo $complete;() The part I want as one result (ie. $complete) is the result of the while loop. There is always at least one result but sometimes 10 which means that it creates 10 table rows. I need to do it this way as later on in the page I use a pdf converter which does not allow loop checks within it otherwise I would just place it within the converter. Hello Everyone, I have a quick question for you all, I think its fairly simple... I have created a database and I am using PHP to grab the data: $usera = $_SESSION['username']; $query2 = "SELECT * FROM tracker WHERE id = '$usera', hidden = yes"; mysql_query($query2) or die('Error, query failed : ' . mysql_error()); This hopefully will return multiple rows which look like this in the database. id username date reps hidden 1 supremebeing 2011-01-02 30 yes 4 supremebeing 2011-04-02 46 yes How would i turn each result into a variable eg: $date1 = 2011-01-02; $date2 = 2011-04-02; $reps1 = 30; $reps2 = 46; I think i have explained that well enough for you to understand, please reply if not though and i will provide more information. Thanks in Advance Hello, I am creating a class which contains one private function that deals with connecting to a SQL Server and executing the sql that is passed in to that function. I have defined a class variable which is assigned the value of sqlsrv_query like so. $this->QueryResult = sqlsrv_query($conn,$sql); I have placed private $QueryResult at the top of my class. The other public methods call this private function to assign the result set to the class variable, the private method returns and then the public methods loop through the results array like so. while($row = sqlsrv_fetch_array($this->QueryResult)) .. but the while loop never gets entered. If I declare everything in the same method then it works, however there are going to be several public methods that will use this private method and I don't want to duplicate all the database coding. Hope someone can help I have found postings close, but not close enough to find my error. I am looking up data from a MySql table and putting it in a dropdown box on a form. I can select the item, but apparently not really. I am not able to echo it, or post it to a record. I'm sure I am missing something simple, but... Code attached if anyone can show me the errors of my ways. Thank you. When I run 'select 1700-price as blah from goldclose as t2 order by dayid desc limit 1' by itself in mysql I get a numerical result: one row, one column. In my php script, the 1700 is actually a variable. so here it is $changequery = sprintf("select $goldprice-price as change from goldclose order by dayid desc limit 1"); $change = mysql_query(changequery); while ($row = mysql_fetch_array($change)) { printf("$row[0]"); } mysql_free_result($changeresult); I get the following error, Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/root/fuzzy/htmlmain4.php on line 99 Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in <b>/root/fuzzy/htmlmain4.php on line 103 Not sure why? All i want is to get the result of that select statement into a variable such as $change Quote i need to store a variable from database like if i have "copies" in one of my column in my database then i have to store a particular value for copies store it to $copies here i want that i can store value of copies into $copies $update_book="update book set copies=copies-1 where bookid='$bookid'"; $result=mysql_query($update_book,$linkID1); if($result) { print "<html><body background=\"header.jpg\"> <p>book successfully subtracted from database</p></body></html>"; } else { print "<html><body background=\"header.jpg\"> <p>problem occured</p></body></html>"; } } This is driving me crazy .... Need to use a variable for the database name on this code ... Code: [Select] <!DOCTYPE html> <html> <head> <title>Table Definition's Tool</title> <style type="text/css"> th { font-size: 110%; border-bottom: 2px solid black; } td { padding: 3px; border-bottom: 1px solid #aaa } </style> </head> <body> <h1>ITG's table's definition</h1> <table> <?php error_reporting (E_ALL ^ E_NOTICE); //Variables $instance=$_REQUEST['instance']; $database_name= $_REQUEST['database_name']; $table=$_REQUEST['table']; require 'utils.php'; // Connect via Windows authentication $server = $instance; $connectionInfo = array( 'CharacterSet' => 'UTF-8' ); $db = sqlsrv_connect($server, $connectionInfo); if ($db === false) { exitWithSQLError('Database connection failed'); } /* Set up and execute the query. */ $query = "SELECT COLUMN_NAME, ORDINAL_POSITION, COLUMN_DEFAULT, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH FROM $database_name.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='$table'"; // Run query $qresult = sqlsrv_query($db, $query); if ($qresult === false) { exitWithSQLError('Query of product data failed.'); } echo '<tr><th>COLUMN NAME</th><th>POSITION</th><th>DEFAULT</th><th>TYPE</th><th>LENGHT</th></tr>'; // Retrieve individual rows from the result while ($row = sqlsrv_fetch_array($qresult)) { echo '<tr><td>', htmlspecialchars($row['COLUMN_NAME']), '</td><td>', htmlspecialchars($row['ORDINAL_POSITION']), '</td><td>', htmlspecialchars($row['COLUMN_DEFAULT']), '</td><td>', htmlspecialchars($row['DATA_TYPE']), '</td><td>', htmlspecialchars($row['CHARACTER_MAXIMUM_LENGTH']), "</td></tr>\n"; } // null == no further rows, false == error if ($row === false) { exitWithSQLError('Retrieving schema failed.'); } // Share Release result liststatement resource and close connection sqlsrv_free_stmt($qresult); sqlsrv_close($db); ?> </table> </body> </html> Is a simple code where the main MS-SQL query is Code: [Select] SELECT COLUMN_NAME, ORDINAL_POSITION, COLUMN_DEFAULT, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH FROM $database_name.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='$table' The user should be able to introduce the database and table's name and above query will display the table's schema. However, does not work. I am able to use a variable for the table, but the program does nothing when I use $database_name inside the query. What am I doing wrong? Help, please .... I'm stumped on what's causing this problem. $descriptor2 is entering "0". But $_POST['States'] does have a value. I put $testable in as a variable to test that in place of descriptor2 and it works just fine. Code: [Select] $descriptor1 = isset($_GET['id']) ? (int)$_GET['id'] : null; $descriptor2 = isset($_POST['States']) ? (int)$_POST['state'] : null; $descriptor3 = isset($_POST['continent_regions']) ? (int)$_POST['continent_regions'] : null; $descriptor4 = isset($_POST['Continents']) ? (int)$_POST['Continents'] : null; $descriptor5 = isset($_POST['Country']) ? (int)$_POST['Country'] : null; $testable = $_POST['States']; // my test variable $query = "INSERT INTO plant_locations_link(plant_id,state,continent_regions,continents,country) VALUES ($descriptor1,$descriptor2,$descriptor3,$descriptor4,$descriptor5)"; // if I change descriptor2 to testable, it inputs the number I need. Hi, I'm trying to use a variable- "$newdbname" in a script that sets up a new database. I'm using this variable because I have an include file that sets all login info and the database name as variables. here's the code. or at lest the relevant part. <?php require 'dbinfo.inc.php'; $db = mysql_connect($servname,$dbusername,$dbpassword) or die ('Unable to connect. Check your connection parameters.'); mysql_select_db($database, $db) or die(mysql_error($db)); // create the user table $query = 'CREATE TABLE ($newdbname)( user_id int(30) NOT NULL AUTO_INCREMENT, username varchar(20) NOT NULL, password varchar(41) NOT NULL, PRIMARY KEY (user_id) ) ENGINE=MyISAM'; mysql_query($query, $db) or die (mysql_error($db)); if I run the script like this, I get the following 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 '($newdbname)( user_id int(30) NOT NULL AUTO_INCREMENT, usernam' at line 1 if I run the code as this, without parentheses around the variable, it creates a table named "$newdbname" <?php require 'dbinfo.inc.php'; $db = mysql_connect($servname,$dbusername,$dbpassword) or die ('Unable to connect. Check your connection parameters.'); mysql_select_db($database, $db) or die(mysql_error($db)); // create the user table $query = 'CREATE TABLE $newdbname ( user_id int(30) NOT NULL AUTO_INCREMENT, username varchar(20) NOT NULL, password varchar(41) NOT NULL, PRIMARY KEY (user_id) ) ENGINE=MyISAM'; mysql_query($query, $db) or die (mysql_error($db)); what am I doing wrong? I have a table that has 5 columns Quote player_id fname lname team I'm trying to get all the values from that table using this sql command Code: [Select] include_once('../database_connection.php'); $player_id = addslashes($_REQUEST['edit_player']); $sql_query = "Select * from nba_info where player_id = '$player_id'"; $result = MYSQL_QUERY($sql_query); "edit_player" above is came from a different page. They I'm fetching the data using while Code: [Select] while($row = mysql_fetch_array($result)) { $row['player_id']; $row['fname']; $row['lname']; $row['team']; $row['email']; } Then I'm trying to pass that values to a variable here Code: [Select] $id = $row['player_id']; $fname = $row['fname']; $fname = $row['lname']; $lname = $row['team']; $email = $row['email']; When I'm trying to put that variables in a textbox value, they are not showing up Here Code: [Select] <table> <form name="edit_player_form" method="post" action=""> <tr> <td>Players ID</td> <td><input type="text" name="playerid" size="20" value="<?php echo $id; ?>" /></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="fname" size="20" value="<?php echo $fname; ?>" /></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lname" size="20" value="<?php echo $lname; ?>" /></td> </tr> <tr> <td>Team</td> <td><input type="text" name="team" size="20" value="<?php echo $team; ?>" /></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" size="20" value="<?php echo $email; ?>" /></td> </tr> <tr> <td><div align="center"> <input type="submit" name="Submit" value="Edit This Player"> </div></td> </tr> </form> </table> I wonder why it ain't showing up on the textbox value? tried almost everything... Anyone? Hey all Im working on an assignment for school and currently I am trying to inser the variable $uid which currently = 2.. But for someone reason when the post happens it inserts a 0 instead of a 2. Here is my insert Code: [Select] mysql_query( "INSERT INTO blog_posts (title, post, author_id, date_posted) ". "VALUES ('$btitle', '$bpost', '$uid', CURDATE())" ); Quote i want to store a value from a database and use it as variable in php code can anyone help me out in this code i want to store value of copies in a php variable and want that it should be more than 0 (zero) for furthur calculations $update_book="update book set copies=copies-1 where bookid='$bookid'"; $result=mysql_query($update_book,$linkID1); if($result) { print "<html><body background=\"header.jpg\"> <p>book successfully subtracted from database</p></body></html>"; } else { print "<html><body background=\"header.jpg\"> <p>problem occured</p></body></html>"; } I am trying to take a variable from a database. But it keeps equaling nothing. Ive tried using mysql_error() and sI get nothing. I also keep tryed mysql_num_rows() and I get 3(just what Id exspect). Here is the code. Code: [Select] <?php if($userb) { $q1 = " SELECT sub FROM login_info WHERE user='$user_log' LIMIT 1 "; $s1 = mysql_query($q1); $scribe = mysql_fetch_assoc($s1); $sub = explode('![sep]!' ,$scribe['sub']); foreach($sub as $key => $name) { $q2 = " SELECT * FROM subscribe WHERE user = '$name' "; echo 'Llama'; $s2 = mysql_query($q2); $num1 = mysql_num_rows($s2); echo '<h3>' .$name .' Posted ' .$num1 .' Storie(s)! </h3>'; while($stor = mysql_fetch_assoc($s2)); { $story = $stor['stor']; $sid = $stor['sid']; $q3 = " SELECT * FROM story_info WHERE id = $sid "; echo $q3 .'<br />'; $s3 = mysql_query($q3) or die(mysql_error()); $rows = mysql_fetch_assoc($s3); $viewsdb = $rows['views']; $titledb = $rows['title']; $userdb = $rows['user']; $catdb = $rows['cat']; $ratdb = $rows['rating']; $id_db = $rows['story_id']; $sumdb = shorten($rows['sum']); echo "<h3><a href='?p=page&id=$id_db'> $titledb </a> </h3>"; echo "<div id='fun_info'>"; echo "$sumdb <br />"; echo "By <a href='?p=profile&user=$userdb'> $userdb </a> <br /> "; echo "$viewsdb Views | Rated: $ratdb | Catagory: <a href='?p=cat_view&gen=$catdb'> $catdb </a> </div>"; } } } else { login('?p=sub'); } Hello Guys, I want to insert an array variable into the database e.g Code: [Select] foreach($html->find('div[class=PostContent]') as $element) { echo $element; $sq = "INSERT INTO articles(original_text) VALUES ('$element') WHERE article_link='$item_url'"; $result = mysql_query($sql1) or die('Query failed: ' . mysql_error()); } i want to insert the variable $element into the database but i'm not able to do so for some reason! $element contains only a paragraph of text. how can i insert the variable $element? Hello guys,
I do not know if anyone work or have worked with the "elFinder (file manager)" .. I incorporate "elFinder" to my platform .. I have the following question .. Have its connector: <?php error_reporting(0); // Set E_ALL for debuging include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderConnector.class.php'; include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinder.class.php'; include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeDriver.class.php'; include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeLocalFileSystem.class.php'; // Required for MySQL storage connector include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeMySQL.class.php'; // Required for FTP connector support include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeFTP.class.php'; /** * Simple function to demonstrate how to control file access using "accessControl" callback. * This method will disable accessing files/folders starting from '.' (dot) * * @param string $attr attribute name (read|write|locked|hidden) * @param string $path file path relative to volume root directory started with directory separator * @return bool|null **/ function access($attr, $path, $data, $volume) { return strpos(basename($path), '.') === 0 // if file/folder begins with '.' (dot) ? !($attr == 'read' || $attr == 'write') // set read+write to false, other (locked+hidden) set to true : null; // else elFinder decide it itself } $opts = array( // 'debug' => true, 'roots' => array( array( 'driver' => 'LocalFileSystem', // driver for accessing file system (REQUIRED) 'path' => '/home/', // path to files (REQUIRED) 'URL' => dirname($_SERVER['PHP_SELF']) . '/home/', // URL to files (REQUIRED) 'accessControl' => 'access' // disable and hide dot starting files (OPTIONAL) ) ) ); // run elFinder $connector = new elFinderConnector(new elFinder($opts)); $connector->run();I want him to make the call the "$screen" (folder will be created automatically by the user) of each user .. and not the "/home/" (as an example of put) .. the "$screen" comes from my platform, and I add the "elFinder" to it .. Imagine that each user creates "1/2/3 or 4, etc .." folders .. Hello all! I am somewhat new to PHP and SQL, but I think I have a basic understanding of how the language works. I have read through a million different websites and forums, and have tried a million different ways but am just not getting anywhere! This is what I am trying to do... I have a text link that I want to click through to an outside URL from my website. This outside URL was already entered into an SQL database, and I just cant seem to get the text link to bring up the outside URL from the database. This is what I have so far... 1.) My webpage that I am working on is www.mywebsite.com/articles.php 2.) I want the text "Read more..." on this webpage to click through to an outside URL, like "http://www.google.com", which was already entered into an SQL database under the title of "article_link" in a new window 3.) So I would like for "Read more..." to click through the outside URL that was entered into "article_link" in the database Code: [Select] <?php echo <p><a href="\' . $article_link . '\" target="_blank">Read more...</a></p>; ?> Nothing I have tried has worked so far, nothing clicks through to the outside URL in the "article_link" entry from the database. Sorry if I am being redundant, I have been at this all day! Please help and thank you in advance!! |