Jump to content

Nodral

Members
  • Posts

    397
  • Joined

  • Last visited

Posts posted by Nodral

  1. Hi

     

    I have just set up a hosted site and cannot connect to the MySQL DB. 

     

    I have set up the correct DB and User but it just keeps failing at the 1st attempt

     

    My code is

    <?php
    session_start();
    $link = mysql_connect('localhost', 'styles', '******');
    if (!$link)
    {
    echo'1Unable to connect to the database server.';
    echo mysql_errno($link) . ": " . mysql_error($link). "\n";
    
    
    exit();
    }
    
    if (!mysql_set_charset('utf8', $link))
    {
    echo'2Unable to connect to the database server.';
    echo mysql_errno($link) . ": " . mysql_error($link). "\n";
    
    
    exit();
    }
    
    if(!mysql_select_db('learning_styles', $link))
    {
    echo'3Unable to connect to the database server.';
    echo mysql_errno($link) . ": " . mysql_error($link). "\n";
    
    
    exit();
    }
    
    ?>
    

     

    All I get output is

    1Unable to connect to the database server.:

     

    But no error message.  Any ideas?

  2. Really straightforward.

     

    Set up your db table (data) with a column called ip_addresses

    Then retrieve them all and populate array

    $sql="SELECT ip_addresses FROM data";
    $sql=mysql_query($sql);
    while($row=mysql_fetch_array($sql)){
    $ip_array[]=$row['ip_addresses'];
    }
    

     

    You now have an array containing all allowed ip addresses. ($ip_array)

    Now if you run a test

    if(in_array($user_ip)){
    echo"IP is fine";
    }
    else
    {
    echo"get lost!!!"
    }
    

  3. Hi

     

    In php ' ' and " " do have a very subtle difference.

     

    if you were to use the following

    $output="Hello World";
    echo "I want to say $output";
    

     

    You would see

    I want to say Hello World
    

     

    However if you put

    $output="Hello World";
    echo 'I want to say $output';
    

     

    You would see

    I want to say $output
    

     

    Also you can mix and match to output SQL or HTML

     

    eg

    $output='<div class="test">Hello World</div>";
    

     

    or

    $sql="INSERT INTO table SET VALUES column1='data'";
    

     

    Hope this helps!!

     

    ;D

     

     

     

  4. Try this

     

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Week 1 Lab</title>
    </head>
    <body>
    <?php
    if (isset($_POST['btn_submit']))	{
    $number_of_rows = $_POST['number_of_rows'];
    echo "<table cellpadding='10' cellspacing='10' border='1'";
    echo "<tr>";
    for($ii = 0; $ii < $number_of_rows; $ii++)		{
    	echo "<tr>";
    	for ($i=0; $i<$number_of_rows; $i++)		{
    		$my_no = mt_rand(1, 100);
    		$average_calc[]=$my_no;
    		if (($my_no%2==0)&&($my_no%3==0)) {
    			$color="#FF00FF";
    		}
    		else if ($my_no%2==0) {
    			$color="#0000FF";
    		}
    		else if ($my_no%3==0) {
    			$color="#FF0000";
    		}
    		else 
    		{	
    			$color ="#FFFFFF";	
    		}				
    		echo "<td style=\"background: {$color};\">$my_no</td>";
    	}
    	echo "</tr>";
    }
    echo "</table>";
    }
    
    $total=array_sum($average_calc);
    $count=count($average_calc);
    $average=$total/$count;
    
    ?>	
    <form name="lab1" method="post" action="">
    <input type="textbox" name="number_of_rows" value="5" />
    <input type="submit" name="btn_submit" value="Generate Grid" />
    <?php
    echo $average;
    ?>
    </form>
    </body>
    </html>
    

     

    I've added in

     

    line 16 to create an array

     

    Lines 37 - 39 to calculate the average

     

    Lines 45 - 47 to print on screen

  5. Hi

     

    Every time your script generates a number, get it to allocate into an array (eg $average_calc), then use the following.

     

    $total=array_sum($average_calc);

    $count=array_count($average_calc);

    $average=$total/$count;

     

     

  6. You could try

    $username = mysqli_real_escape_string($dbc, $_POST['username']);
    $password = mysqli_real_escape_string($dbc, $_POST['password']);
    

     

    In the php manual, there is no requirement for the set of brackets around the string you are converting.

     

    Sorry if this is wrong, but I'm not really that experienced with mysqli

  7. Using mysql

     

    Set up a file called connect.php with the following.

     

    <?php
    
    $link = mysql_connect('localhost', 'username', 'password');
    if (!$link)
    {
    echo'1Unable to connect to the database server.';
    
    exit();
    }
    
    if (!mysql_set_charset('utf8', $link))
    {
    echo'2Unable to connect to the database server.';
    
    exit();
    }
    
    if(!mysql_select_db('db-name', $link))
    {
    echo'3Unable to connect to the database server.';
    
    exit();
    }
    
    ?>
    

     

    Then use include_once('connect.php'); at the start of every script requiring a DB connection.

     

    Then when using try using $username=mysql_real_escape_string($_POST['username'];

     

    Note there is no link or connection identifyer in the brackets.

     

     

  8. I was in a similar situation where I was using mysqli in dev and mysql in prod and this caused me no end of issues as the commands are different, sometimes subtly and sometimes quite obviously.

     

    I'd ask your host to use whatever you are using n dev as this will save you loads of time in debugging, alternatively you use the same in dev as you are using in live.

     

    What error did you get in prod when you used mysql_real_escape_string?

  9. My advice is to save yourself hours of dev work and look for a pre-built mail script.

     

    I use Rmail which can be found from Google and this lets you send all sorts of emails simply by setting a few variables and then using an include.

     

     

  10. All you do is put a line in submitAction.php which would process the variable and assign it.

     

    eg. if your $_POST variable was called test. you do the following.

     

    $_SESSION['test']=$_POST['test'];

     

    Then provided you have declared session_start(); at the beginning of every script, you can use $_SESSION['test'] in every script after it has been set.

  11. Hi all

     

    I had this working in a dev environment with php 5.2.13, however I have moved it into live which used php 5.3.6 and I'm having issues with my elseif statement.

     

    <?php
    //has user agreed to have admin account?  If confirm has not yet been set...........
    if(!isset($_POST['confirm'])){
    include('header.php');
    unset($_SESSION['dbsecure']);
    		echo'<div class="pagetext">You are the first to use this application.<br>Would you like to be set up as the main admin user?<br></div>
    		<form method="POST" action="">
    		<input type="submit" name="confirm" value="Yes"><input type="submit" name="confirm" value="No">
    		</form>';
    
    //if confirm is set, has form been completed?	
    }
    elseif(isset($_POST['change'])){
    
    //if form is completed are all variables set?
    if(isset($ED, $password, $firstname, $lastname, $email, $dept, $workplace, $home, $role)){
    
    	//create table
    	$sql="CREATE TABLE pfp_user (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, ed VARCHAR(255) NOT NULL, firstname VARCHAR(255) NOT NULL, lastname VARCHAR(255) NOT NULL, dept VARCHAR(255), workplace VARCHAR(255), home VARCHAR(255), password VARCHAR(255), email VARCHAR(255), role VARCHAR(255) NOT NULL, complete VARCHAR(255) NOT NULL, type VARCHAR(255)) DEFAULT CHARACTER SET utf8";
    	mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
    
    	//hash password
    	$password=md5($password);
    
    	//insert data
    	$sql="INSERT INTO pfp_user (ED, firstname, lastname, dept, workplace, home, password, email, role, complete, type) VALUES ('$ED', '$firstname', '$lastname', '$dept', '$workplace', '$home', '$password', '$email', '$role', 'Yes', 'Allocated')";
    		mysql_query($sql) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
    		$_SESSION['output']="Admin User Successfully Registered.  Please log in.";
    		$extra = 'index.php';
    		header("Location: http://$host$uri/$extra");
    
    
    }
    //missing data
    else{
    	$_SESSION['output']='<div class="pagetext">You have missed some information<br>Please try again</div>';
    	}
    }
    

     

    I get the following error

     

    PHP Parse error:  syntax error, unexpected '}' in /home/httpd/vhosts/client9/web14/web/devsite/pfp/config.php on line 62

     

    Line 62 is the elseif statement.  Any ideas?

  12. Here's an update,

     

    if I use D:/Documents/AI24/Web/tmp as $filename it works.  However I need this to be a relative path for my production server as I don't know the full root path

  13. Using Apache, Windows XP and PHP 5.2.13

     

    My web host is the same except for they use php 5.3.

     

    I'm unsure how to set up read/write access on the folder, but I've never had an issue with any other folders I've created through windows

  14. ok, once more

     

    <?php
    $fp = fopen('test.csv','r');
    if (!$fp) {echo 'ERROR: Unable to open file.</body></html>';
    exit;
    }
    $row_count = 0;
    while (!feof($fp)) {
    // this is to exclude the first row
    if($row_count==0){fgets($fp, 2048);
    //use 2048 if very long lines
    $row_count++;continue;
    }
    $line = fgets($fp, 2048); 
    //use 2048 if very long lines
    if (!empty($line)){list ($id, $offer, $subh, $desc, $promo, $notes ) = explode('\|', $line);
    $output[$id]=$offer.",". $subh.",".  $desc.",".  $promo.",".  $notes;
    }
    $fp++;
    }
    fclose($fp);
    $output=array_reverse($output);
    //template start
    while(list($id, $values)=each($output)){
    	list ($offer, $subh, $desc, $promo, $notes ) = explode (',', $values);
    	echo"$id<center><h3>$offer</h3><h4>$subh</h4></center><p align='left'>$desc</p>Please quote Promo Code:<div align center> <b>$promo</b></div><hr>";
    
    	//template end
    	}
    ?>
    

     

    You should really replace split with explode for this as split is now deprecated

  15. try

    <?php
    $fp = fopen('bbs.txt','r');
    if (!$fp) {echo 'ERROR: Unable to open file.</body></html>'; exit;}
    $row_count = 0;
    while (!feof($fp)) {
    // this is to exclude the first row
    if($row_count==0){
    fgets($fp, 2048); //use 2048 if very long lines
    $row_count++;
    continue;
    
    }
    $line = fgets($fp, 2048); //use 2048 if very long lines
    if (!empty($line)){
    list ($id, $offer, $subh, $desc, $promo, $notes ) = split ('\|', $line);
    $output[$id]=$offer. $subh. $desc. $promo. $notes;
    }
    $fp++;
    }
    fclose($fp);
    
    $output=array_reverse($output);
    //template start
    while(list($id, $values)=each($output)){
    list ($offer, $subh, $desc, $promo, $notes ) = split (',', $values);
    aprint"$id
    <center><h3>$offer</h3>
    <h4>$subh</h4></center>
    <p align='left'>$desc</p>
    Please quote Promo Code:<div align center> <b>$promo</b></div><hr>";
    //template end
    }
    ?>
    

     

    Effectively this will load the file and close it, reverse the array then output each value.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.