PHP - Help Needed In Database Connection
Hi guys,
Similar Tutorialsafter cloasing connection of database i still got the values form database. Code: [Select] <?php session_start(); /* * To change this template, choose Tools | Templates * and open the template in the editor. */ require_once '../database/db_connecting.php'; $dbname="sahansevena";//set database name $con= setConnections();//make connections use implemented methode in db_connectiong.php mysql_select_db($dbname, $con); //update the time and date of the admin table $update_time="update admin set last_logged_date =CURDATE(), last_log_time=CURTIME() where username='$uname'limit 3,4"; //my admin table contain 5 colums they are id, username,password, last_logged_date, last_log_time $link= mysql_query($update_time); // mysql_select_db($dbname, $link); //$con=mysql_connect('localhost', 'root','ijts'); $result="select * from admin where username='a'"; $result=mysql_query($result); mysql_close($con); //here i just check after closing data baseconnection whether i do get reselts but i do, why? echo "after the cnnection was closed"; if(!$result){ echo "cont fetch data"; }else{ $row= mysql_fetch_array($result); echo "id".$row[0]."usrname".$row[1]."passwped".$row[2]."date".$row[3]."time".$row[4]; } // echo "<html>"; //echo "<table border='1' cellspacing='1' cellpadding='2' align='center'>"; // echo "<thead>"; // echo"<tr>"; // echo "<th>"; // echo ID; // echo"</th>"; // echo" <th>";echo Username; echo"</th>"; // echo"<th>";echo Password; echo"</th>"; // echo"<th>";echo Last_logged_date; echo "</th>"; // echo "<th>";echo Last_logged_time; echo "</th>"; // echo" </tr>"; // echo" </thead>"; // echo" <tbody>"; //while($row= mysql_fetch_array($result,MYSQL_BOTH)){ // echo "<tr>"; // echo "<td>"; // echo $row[0]; // echo "</td>"; // echo "<td>"; // echo $row[1]; // echo "</td>"; // echo "<td>"; // echo $row[2]; // echo "</td>"; // echo "<td>"; // echo $row[3]; // echo "</td>"; // echo "<td>"; // echo $row[4]; // echo "</td>"; // echo "</tr>"; // } // echo" </tbody>"; // echo "</table>"; // echo "</html>"; session_destroy(); session_commit(); echo "session and database are closed but i still get values from doatabase session is destroyed".$_SESSION['admin']; ?> session is destroyed but database connection is not closed. thanks I'm having difficulties forming a google query to get a result I want. Basically I want to have a db connection used by other classes. Someone showed me a month ago about a way to pass a db object to the class but said it wasn't the best method. So I'm trying to find a tutorial to get me on the right track. I'm new to OOP so if this sounds stupid I apologize. This is what I'm currently doing (passing the db connection to the object): Code: [Select] <?php //config file $db = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME); //page $user = new user($db); //user class public function __construct($db){ $this->db = $db; } ?> Any help in the right direction would be greatly appreciated. Also if anyone knows any good books to purchase on walking through small projects that would be awesome. I bought PHP5 Objects, Patterns and Practices but that didn't really walk through a project. It did get to some complicated things (for me) that I think I can understand once I get smaller projects under my belt. Thanks, Justin the notebook lenovo g 780 runs windows 8 and is not very stable. the WLAN connecgtion is not very stable _ despite the fact that it is very very close to the router . btw: several linux-notebooks (opensuse) which are running under the same condition do not have any problem want to load a new driver - but which one is corrcet? how to find out which driver is needed? http://www.lenovo-dr...rivers-download Networking: LAN (Ethernet) Atheros LAN Driver Windows 8 (32-bit) Windows 8 (64-bit) 3.17MB Download Networking: LAN (Ethernet) Atheros Lan Driver Windows XP 3.02MB Download Networking: LAN (Ethernet) Atheros LAN Driver Windows 7 (32-bit) Windows 7 (64-bit) 5.33MB Download Networking: Wireless LAN Intel WiMax Driver Windows XP 64MB Download Networking: Wireless LAN Wireless LAN Driver (Liteon, CyberTan) Windows 8 (32-bit) Windows 8 (64-bit) 49.4MB Download Networking: Wireless LAN Intel WLAN Driver Windows XP 203MB Download Networking: Wireless LAN Intel WiMax Driver Windows 8 (32-bit) Windows 8 (64-bit) 88.7MB Download Networking: Wireless LAN WLAN Driver (Liteon, Cybertan) Windows XP 41.8MB Download Networking: Wireless LAN Intel Wireless LAN Driver Windows 8 (32-bit) Windows 8 (64-bit) 540KB Download how to find out which driver is needed? hey i need help im tryig to get information from my user and then process it in my database so i can use it to log them to a different web site im trying to use this method to get the information from the user but need help to get it please help me. Code: [Select] //make the database connection. $conn = mysql_connect("localhost", "Black Jack"); mysql_select_db("chaper7", $conn); //create a query $sql = "SELECT * FROM hero"; $result = mysql_query($sql, $conn); On some occasions I need to connect to a second and third database in the same script (maybe 5% of scripts have at least a second connection). Usually I would just select the new database. However, my host requires different users to be created for each database. What is the best way to do this? Close current connection (say db1) and open new (say db2) OR keep all open, creating 2nd and 3rd connections. I am happy with the design of my database, and don't want to merge all these tables into one db. Overall I am still happy with my host, so I'd rather not change. <?php class UserQuery { public function Adduser($id,$username,$email,$password) { $conn = new Config(); $sql =("INSERT INTO test.user (id, username, email, password) VALUES ('$id', '$username', '$email',$password)"); $conn->exec($sql); } }
getting an "exec doesnt exist " error, saying exec doesnt exist in my db file. it doesnt need to exist does it ? anyone any idea why ?
Through out my program I have used global $mysqli; as a connection to my database - this has worked fine so far. Now I have called $sql_statement = "SELECT * FROM items WHERE name='$itemName'"; $itemStats = mysqli_fetch_array(mysqli_query($mysqli, $sql_statement), MYSQLI_ASSOC); via a function and I get the following warnings - Quote Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /functions/getdata.php on line 27 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /functions/getdata.php on line 27 it still works though and returns the correct information. If I take the code out of the function and use it normally then I get no Warnings - but this defeats the whole purpose of having functions! How do I get rid of the warnings? (and no, I don't mean turn the warnings off ) Hi, For school, I need to build a site and this site has to have a few applications. One of the applications, I have to make is to make a script that makes it possible to upload a file and it has to put this file in a online database. Now, I'm just trying to put it in a local database. My code doesn't give an error, but it doesn't work either. The file doesn't get into the database. I can't figure out why. I hope you can help me? Thanks in advance! Lize Hey,
I'm really new to PHP and having some difficulties with $_SESSION and getting userid from the database. I've managed to put content to my database and also a login script. Though, adding sessions has been a pain. Here's what I got so far:
$sql = "SELECT username, password FROM users WHERE username = '$username' and password = '$pas'"; $query_login = $db->prepare($sql); $query_login->execute(array('userid' => $userid, 'username' => $username, 'password' => $pas)); $result = $query_login->rowcount(); if ($result>0) { session_start(); $_SESSION['username'] = $username; $_SESSION['logged'] = 1; $_SESSION['userid'] = $result['userid']; header('Location: ../user/user.php'); } I'm connecting to my database using the following... @ $db = new mysqli('host', 'username', 'password', 'database') The .php file that is connecting to the database is in my root (htdocs) folder on the server. I know that I am not supposed to put my actual 'host', 'username', 'password', 'database' inside the mysqli function for security purposes. I know that I am supposed to put variables in instead. But here is where I am confused. Where do I set those variables? Do I set them in another file and include that file? If so, where do I store the file that holds the passwords, and what prevents a hacker from simply navigating to that file? Thanks for the help how do I use a connection file (connection.php) in multiple programs? I think include? Hello guys. Trying to connect php with mysql database and then display results on the screen. This is my code: Code: [Select] <?php $dbhost = "localhost"; $dbuser = "username1"; $dbpass = "password1"; $db = "username1_myDB"; $connection = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Could not connect"); mysql_select_db($connection, $db); $show = "SELECT Name, Description FROM people"; $result = mysql_query($show); while($show = mysql_fetch_array($result)){ $field01 = $show[Name]; $field02 = $show[Description]; echo "id: $field01<br>"; echo "description: $field02<p>"; } ?> However im getting this: Warning: mysql_select_db() expects parameter 1 to be string, resource given in /home/pain33/public_html/index.php on line 20 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/pain33/public_html/index.php on line 26 Any ideas how to fix this? Thank you. When you open a database connection, how long does it stay open for? I have a "Change E-mail Address" script that has 4 queries in it, and I just realized that I don't create a DB Connection until Query #2 in my script, yet things work fine?! Is it possible that the DB Connection was opened earlier by another script and it is just persisting?? Debbie I get the following error when trying to connect Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on '72.18.129.104' (10061) in C:\Domains\crysvis.com\wwwroot\include\dbConnectAndSelect.php on line 8 Here's the code $host = "72.18.129.104"; $user = "deltron"; $pass = "masterconn"; $db = "customers"; $conn = mysql_connect("$host","$user","$pass"); if(!$conn) errorHandler("Msg 10:\nCould not connect to database with $host,$user,$pass in ". $_SERVER["PHP_SELF"]."\nCustId = $CustId\nmysql_error() = ".mysql_error()); if(!mysql_select_db("$db",$conn)) errorHandler("Msg 11:\nCould not select db=$db in ". $_SERVER["PHP_SELF"]."\nCustId=$CustId\nmysql_error() = ".mysql_error()); ?> What am I doing wrong? Any help will be appreciated Dear friends i,m a php beginner and i got a problem with connecting to my database i created a database called (koora) with one table called (admins) and when i tried to connect to it (database ) ; it did not connect here is the code i used for that <?php $connectdb = mysql_connect('localhost','','') or die("not connected"); $selectdb = mysql_select_db("koora", $connectdb); if(!$selectdb) { die("error connecting table" .mysql_error()); } then when refreshing my phpmyadmin page i got that message error connecting tableAccess denied for user ''@'localhost' to database 'koora' koora is the name of the database so i need your help with this problem and what is the reason not to connect to the data base Thank you Hi, I am currently trying to build an Artist's website, the artist wanted a CMS that was completely customized to the site (so no Wordpress, Joomla, Drupel, etc) - because of this I am having to create a CMS completely from scratch.
The problem I am having is with the database connection (hence the topic title), other sites that I have built with this same code work fine - however this particular site does not seem to want to play ball. It keeps giving me this error:
SQLSTATE[HY000] [1045] Access denied for user 'web113-janesart'@'10.0.44.113' (using password: YES)I have tried obvious things such as spelling mistakes, wrong password/username/db name, nothing seems to get rid of this error. Any help on what else this could be would be appreciated. The Script:
pdo_connect.php:
<?php $dsn = "mysql:host=localhost;dbname=2354"; $db = new PDO($dsn, "root", ""); ?>pdo_test.php: <?php try{ require_once("pdo_connect.php"); }catch(Exception $e){ $error = $e->getMessage(); } ?> <!DOCTYPE html> <html> <head> <title>Database Connection with PDO</title> </head> <body> <h1>Connection with PDO</h1> <?php if($db){ echo "<p>Connection successful.</p>"; }elseif(isset($error)){ echo "<p>$error</p>"; } ?> </body> </html>I have just watched a tutorial and tried out this script. The issue is that I am getting the following notice alongside with the error message: Notice: Undefined variable: db in C:\xampp\htdocs\oophp\pdo_test.php on line 18 SQLSTATE[HY000] [1049] Unknown database '2354'By the way this notice does happen in the tutorial as well. My question is: How to have this in ways, where the notice does not occur? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta name="description" content="Nearest Neighbour Algorithm - Data Mining" /> <meta name="keywords" content="nearest neighbour, data mining, php, computer science, james hamilton" /> <meta http-equiv="Content-Language" content="en-gb" /> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <meta name="robots" content="all" /> <meta name="revisit-after" content="1 days" /> <title>Nearest Neighbour Algorithm - Data Mining11</title> <?PHP echo "<br><left><small>CONNECTION MESSAGES <br>"; echo "<ul>"; echo "<li>establishing connection with database</li>"; $conn = pg_Connect ("port=5432 dbname=my-database user=nobody"); if (!$conn) { echo "<li>connection to database failed</li>" ; exit; } echo "<li>connection to database ok</li>" ; ?> <link rel="stylesheet" media="all" href="style.css" type="text/css" /> <link rel="stylesheet" media="all" href="nearestneighbour.css" type="text/css" /> </head> <body> <?php if(isset($_GET[source])) { highlight_file(__FILE__); }else{ ?> <p>The nearest neighbour algorithm classifies a given instance based on a set of already classified instances (the training set), by calculating the distance to the nearest training case. The new instance is classified in the same class as the closest training case (i.e. the one that has the least differences/the one that is most the same). </p> <p> Try this out below. There are 10 instances in the training set. Use the select boxes to diagnose a new patient using the nearest neighbour algorithm. The diagnosis is made by selecting the diagnosis (class) of the instance in the training set with the least differences (i.e. closest distance). </p> <table id="patients" cellpadding="3" cellspacing="0"> <tr> <th>Patient ID</th> <th>age</th> <th>sex</th> <th>chestpaintype</th> <th>bloodpressure</th> <th>cholesterol</th> <th>fbs120</th> <th>restingegh</th> <th>maxheartrate</th> <th>angina</th> <th>slop</th> <th>thal</th> <th>diagnosis</th> </tr> <?php /* function getRows takes an SQL query string (and a optional array key type - NUM or ASSOC) and returns and array containing all the rows of the query. define(ASSOC, 1); define(NUM, 2); function getRows($sql, $type=ASSOC) { $result = mysql_query($sql); if($type == ASSOC) { while($row = mysql_fetch_array($result)) $ret[] = $row; }else if($type == NUM) { while($row = mysql_fetch_row($result)) $ret[] = $row; }else{ die("type should be NUM or ASSOC"); } return $ret; } */ $rows = getRows("SELECT * FROM heartcondition;", NUM); $closest = -1; if(isset($_POST[diagnose])) { $distance = array_fill(0, count($rows), 0); $instance = $_POST[instance]; for($i = 0; $i < count($rows); $i++) { for($j = 1; $j < 11; $j++) { if($rows[$i][$j] != $instance[$j]) $distance[$i]++; } } $closest = array_search(min($distance), $distance); $diagnosis = $rows[$closest][12]; } $a = 0; foreach($rows as $row) { ?> <tr<?php if($closest == $a) { ?> class="selected"<?php } ?>> <td><?=$row[0]?></td> <td><?=$row[1] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[2] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[3] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[4] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[5] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[6] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[7] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[8] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[9] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[10] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[11] == 0 ? "No" : "Yes"; ?></td> <td><?=$row[12]?></td> <td><?=$distance[$a++]?></td> </tr> <?php } ?> <tr> <form method="post" action="<?=$_SERVER['PHP_SELF']?>"> <td><input type="hidden" name="instance[]" value="11" />11</td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][1] == "0") echo "selected=\"selected\"";?>>No</option> <option value="1"<?php if($_POST[instance][1] == "1") echo "selected=\"selected\"";?>>Yes</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][2] == "0") echo "selected=\"selected\"";?>>Sixties</option> <option value="1"<?php if($_POST[instance][2] == "1") echo "selected=\"selected\"";?>>forties</option> <option value="1"<?php if($_POST[instance][2] == "2") echo "selected=\"selected\"";?>>fifties</option> <option value="1"<?php if($_POST[instance][2] == "2") echo "selected=\"selected\"";?>>thirties</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][3] == "0") echo "selected=\"selected\"";?>>Male</option> <option value="1"<?php if($_POST[instance][3] == "1") echo "selected=\"selected\"";?>>Female</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][4] == "0") echo "selected=\"selected\"";?>>Angina</option> <option value="1"<?php if($_POST[instance][4] == "1") echo "selected=\"selected\"";?>>AbnomalAngina</option> <option value="1"<?php if($_POST[instance][4] == "2") echo "selected=\"selected\"";?>>No tang</option> <option value="1"<?php if($_POST[instance][4] == "3") echo "selected=\"selected\"";?>>Asymptomatic</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][5] == "0") echo "selected=\"selected\"";?>>onethirty+</option> <option value="1"<?php if($_POST[instance][5] == "1") echo "selected=\"selected\"";?>>oneten+</option> <option value="1"<?php if($_POST[instance][5] == "2") echo "selected=\"selected\"";?>>onefourty+</option> <option value="1"<?php if($_POST[instance][5] == "3") echo "selected=\"selected\"";?>>onefifty+</option> <option value="1"<?php if($_POST[instance][5] == "4") echo "selected=\"selected\"";?>>onetwenty+</option> <option value="1"<?php if($_POST[instance][5] == "5") echo "selected=\"selected\"";?>>onesisty+</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][6] == "0") echo "selected=\"selected\"";?>>twoten+</option> <option value="1"<?php if($_POST[instance][6] == "1") echo "selected=\"selected\"";?>>twosixty+</option> <option value="1"<?php if($_POST[instance][6] == "2") echo "selected=\"selected\"";?>>twofifty+</option> <option value="1"<?php if($_POST[instance][6] == "3") echo "selected=\"selected\"";?>>twohundred+</option> <option value="1"<?php if($_POST[instance][6] == "4") echo "selected=\"selected\"";?>>twotwenty+</option> <option value="1"<?php if($_POST[instance][6] == "5") echo "selected=\"selected\"";?>>twoeight+</option> <option value="1"<?php if($_POST[instance][6] == "6") echo "selected=\"selected\"";?>>twothirty+</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][7] == "0") echo "selected=\"selected\"";?>>f</option> <option value="1"<?php if($_POST[instance][7] == "1") echo "selected=\"selected\"";?>>t</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][8] == "0") echo "selected=\"selected\"";?>>Hyp</option> <option value="1"<?php if($_POST[instance][8] == "1") echo "selected=\"selected\"";?>>Normal</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][9] == "0") echo "selected=\"selected\"";?>>onethirty+</option> <option value="1"<?php if($_POST[instance][9] == "1") echo "selected=\"selected\"";?>>oneforty+</option> <option value="0"<?php if($_POST[instance][9] == "2") echo "selected=\"selected\"";?>>onefifty+</option> <option value="0"<?php if($_POST[instance][9] == "3") echo "selected=\"selected\"";?>>onesixty+</option> <option value="0"<?php if($_POST[instance][9] == "4") echo "selected=\"selected\"";?>>oneseventy+</option> <option value="0"<?php if($_POST[instance][9] == "5") echo "selected=\"selected\"";?>>oneeighty+</option> <option value="0"<?php if($_POST[instance][9] == "6") echo "selected=\"selected\"";?>>onetwenty+</option> <option value="0"<?php if($_POST[instance][9] == "7") echo "selected=\"selected\"";?>>onehundred+</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][10] == "0") echo "selected=\"selected\"";?>>f</option> <option value="1"<?php if($_POST[instance][10] == "1") echo "selected=\"selected\"";?>>t</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][11] == "0") echo "selected=\"selected\"";?>>Falt</option> <option value="1"<?php if($_POST[instance][11] == "1") echo "selected=\"selected\"";?>>Up</option> <option value="1"<?php if($_POST[instance][11] == "2") echo "selected=\"selected\"";?>>Down</option> </select> </td> <td><select name="instance[]"> <option value="0"<?php if($_POST[instance][12] == "0") echo "selected=\"selected\"";?>>Rev</option> <option value="1"<?php if($_POST[instance][12] == "1") echo "selected=\"selected\"";?>>Normal</option> <option value="1"<?php if($_POST[instance][12] == "2") echo "selected=\"selected\"";?>>Fix</option> </select> </td> <td<?php if($diagnosis) { ?> class="selected"<?php } ?>><?=$diagnosis?></td> <td><input type="submit" name="diagnose" value="Diagnose" /></td> </form> </tr> </table> <p> <a href="<?=$_SERVER[PHP_SELF]?>?source">View Page Source</a> </p> <?php } ?> <p> Back to <a href="index.php">Data Mining</a> </p> <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> <script type="text/javascript"> _uacct = "UA-1032885-1"; urchinTracker(); </script> </body> </html> is this code correct, i really need help with connecting the database 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 .. |